A production-ready FastAPI web application with multiple endpoints, proper validation, error handling, comprehensive tests, and CI/CD integration.
- FastAPI Framework: Modern, fast web framework for building APIs
- Pydantic Validation: Automatic request/response validation
- Error Handling: Custom error handlers with meaningful responses
- RESTful API: Complete CRUD operations for Users and Items
- Comprehensive Tests: Unit and integration tests with pytest
- CI/CD Pipeline: GitHub Actions workflows for testing and deployment
- Docker Support: Multi-stage Dockerfile and docker-compose setup
- Code Quality: Black, Ruff, and MyPy for formatting and linting
- API Documentation: Auto-generated Swagger/OpenAPI docs
fastapi-webapp/
├── app/
│ ├── __init__.py
│ ├── main.py # Application entry point
│ ├── api/
│ │ ├── endpoints/ # API route handlers
│ │ │ ├── health.py # Health check endpoint
│ │ │ ├── users.py # User CRUD operations
│ │ │ └── items.py # Item CRUD operations
│ │ └── schemas/ # Pydantic models
│ │ ├── user.py
│ │ └── item.py
│ ├── core/
│ │ └── config.py # Application configuration
│ └── middleware/
│ └── error_handler.py # Error handling middleware
├── tests/
│ ├── conftest.py # Test fixtures
│ ├── test_health.py
│ ├── test_users.py
│ └── test_items.py
├── .github/
│ └── workflows/
│ ├── ci.yml # CI pipeline
│ └── cd.yml # CD pipeline
├── requirements.txt # Python dependencies
├── pyproject.toml # Project metadata and tool configs
├── Dockerfile # Docker image definition
├── docker-compose.yml # Docker compose configuration
├── Makefile # Common commands
└── README.md
- Python 3.11 or higher
- pip
git clone <repository-url>
cd fastapi-webapppython -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate# Production dependencies
pip install -r requirements.txt
# Or use make
make installcp .env.example .env
# Edit .env with your configuration# Using uvicorn directly
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
# Or using make
make runThe application will be available at:
- API: http://localhost:8000
- Swagger UI: http://localhost:8000/api/docs
- ReDoc: http://localhost:8000/api/redoc
# Build and run with docker-compose
make docker-run
# Or manually
docker-compose up -d
# Stop containers
make docker-stopGET /api/health- Check API health status
POST /api/users/- Create a new userGET /api/users/- Get all users (paginated)GET /api/users/{user_id}- Get user by IDPUT /api/users/{user_id}- Update userDELETE /api/users/{user_id}- Delete user
POST /api/items/- Create a new itemGET /api/items/- Get all items (paginated, filterable)GET /api/items/{item_id}- Get item by IDPUT /api/items/{item_id}- Update itemDELETE /api/items/{item_id}- Delete item
# Using pytest directly
pytest tests/ -v --cov=app --cov-report=html
# Or using make
make testAfter running tests, open htmlcov/index.html in your browser.
# Format with black
black app/ tests/
# Or using make
make format# Lint with ruff
ruff check app/ tests/
# Type check with mypy
mypy app/
# Or using make
make lintmake help # Show all available commands
make install # Install production dependencies
make dev # Install development dependencies
make test # Run tests with coverage
make lint # Run linters
make format # Format code
make clean # Clean up cache files
make run # Run development server
make docker-build # Build Docker image
make docker-run # Run with docker-compose
make docker-stop # Stop docker containersThis project uses GitHub Actions for CI/CD:
- Runs on push and pull requests to
mainanddevelopbranches - Tests on Python 3.11 and 3.12
- Runs linters (ruff, black)
- Type checking with mypy
- Unit and integration tests
- Code coverage reporting
- Triggers on push to
mainor version tags - Builds and pushes Docker images
- Can be extended with deployment steps
Application configuration is managed through environment variables. See .env.example for available options:
ENVIRONMENT: Application environment (development/production)DEBUG: Enable debug modeSECRET_KEY: Secret key for security featuresDATABASE_URL: Database connection stringALLOWED_HOSTS: CORS allowed hosts
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow PEP 8 guidelines
- Use Black for code formatting
- Use Ruff for linting
- Add type hints where possible
- Write tests for new features
This project is licensed under the MIT License.
For issues and questions, please open an issue on GitHub.
Future enhancements planned:
- Database integration (PostgreSQL/MySQL)
- Authentication and authorization (JWT)
- Rate limiting
- Caching with Redis
- Background tasks with Celery
- WebSocket support
- GraphQL endpoint
- Prometheus metrics
- Logging and monitoring