A production-ready FastAPI application with best practices, security, and scalability in mind.
- ✅ FastAPI - Modern, fast web framework
- ✅ SQLAlchemy - ORM for database operations
- ✅ Alembic - Database migrations
- ✅ PostgreSQL - Production database
- ✅ JWT Authentication - Secure token-based auth
- ✅ Pydantic v2 - Data validation
- ✅ Docker & Docker Compose - Containerization
- ✅ Nginx - Reverse proxy
- ✅ pytest - Testing framework
- ✅ CORS Middleware - Cross-origin support
- ✅ Logging - Structured logging
- ✅ Environment Configuration - Secure config management
fastapi-project/
├── app/
│ ├── api/ # API endpoints
│ ├── core/ # Security, logging
│ ├── crud/ # Database operations
│ ├── db/ # Database config
│ ├── models/ # SQLAlchemy models
│ ├── schemas/ # Pydantic schemas
│ └── main.py # Application entry point
├── tests/ # Test files
├── alembic/ # Database migrations
├── Dockerfile
├── docker-compose.yml
└── requirements.txt
# Clone repository
git clone <your-repo-url>
cd fastapi-project
# Copy environment file
cp .env.example .env
# Edit .env and update values (especially SECRET_KEY and passwords)
nano .env# Build and start all services
docker-compose up -d
# View logs
docker-compose logs -f
# Stop services
docker-compose downThe API will be available at:
- API: http://localhost:8000
- Docs: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Start PostgreSQL (make sure it's running)
# Run migrations
alembic upgrade head
# Start development server
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000# Create a new migration
alembic revision --autogenerate -m "Description of changes"
# Apply migrations
alembic upgrade head
# Rollback one migration
alembic downgrade -1
# View migration history
alembic history# Run all tests
pytest
# Run with coverage
pytest --cov=app --cov-report=html
# Run specific test file
pytest tests/test_api/test_users.py
# Run with verbose output
pytest -vPOST /api/v1/auth/login- Login and get tokens
POST /api/v1/users/- Create new userGET /api/v1/users/me- Get current userPUT /api/v1/users/me- Update current userGET /api/v1/users/- Get all users (admin only)GET /api/v1/users/{user_id}- Get user by ID (admin only)PUT /api/v1/users/{user_id}- Update user (admin only)DELETE /api/v1/users/{user_id}- Delete user (admin only)
GET /health- Health check endpoint
Key environment variables (see .env.example for all):
# Database
POSTGRES_SERVER=localhost
POSTGRES_USER=postgres
POSTGRES_PASSWORD=your_password
POSTGRES_DB=fastapi_db
# Security
SECRET_KEY=your-secret-key-min-32-characters
ACCESS_TOKEN_EXPIRE_MINUTES=30
# Environment
ENVIRONMENT=production
DEBUG=false- Change
SECRET_KEYto a strong random value - Change all default passwords
- Set
DEBUG=false - Configure CORS origins properly
- Set up SSL/TLS certificates
- Enable Nginx rate limiting
- Configure firewall rules
- Set up monitoring and logging
- Regular database backups
- Update dependencies regularly
python -c "import secrets; print(secrets.token_urlsafe(32))"For production, obtain SSL certificates (Let's Encrypt recommended):
# Install certbot
sudo apt-get install certbot
# Get certificate
sudo certbot certonly --standalone -d yourdomain.com
# Update nginx.conf with certificate paths
# Uncomment HTTPS server block in nginx.conf# Docker logs
docker-compose logs -f api
# Local logs
tail -f logs/app.log# Connect to database
docker-compose exec db psql -U postgres -d fastapi_db
# Check connections
SELECT * FROM pg_stat_activity;- Database Indexing: Ensure proper indexes on frequently queried columns
- Connection Pooling: Configured in
db/session.py - Caching: Consider adding Redis for caching
- Rate Limiting: Configured in Nginx
- Workers: Adjust Uvicorn workers based on CPU cores
- Fork the repository
- Create feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open Pull Request
This project is licensed under the MIT License.
For issues and questions, please open an issue on GitHub.