A production-ready FastAPI boilerplate designed for rapid project setup β featuring clean architecture, Docker support, CI/CD, logging and INI-based configuration.
- β‘ FastAPI with Python 3.13
- π³ Docker & Docker Compose for development and production
- π CI/CD pipeline with GitHub Actions
- ποΈ PostgreSQL database with SQLAlchemy
- π΄ Redis for caching
- π Nginx reverse proxy with rate limiting
- π Alembic for database migrations
- π§ͺ Pytest for testing
- π Logging configured and ready to use
- π§ Makefile for convenient development
- Docker and Docker Compose
- Python 3.13+ (for local development)
- Make (optional)
- Clone the repository:
git clone <your-repo-url>
cd fastapi-template- Create
config.inifile from example:
cp config.ini.example config.ini- Edit
config.inifile according to your needs
make dev
# or
docker compose -f docker/docker-compose.dev.yml up --buildpython -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt
python src/main.py
# or
uvicorn src.main:app --reloadApplication will be available at: http://localhost:8000
API documentation:
- Swagger UI: http://localhost:8000/api/docs (protected)
- OpenAPI JSON: http://localhost:8000/api/openapi.json
make up
# or
docker compose -f docker/docker-compose.yml up -dfastapi-template/
βββ .github/
β βββ workflows/
β βββ develop.yaml # CI/CD pipeline
βββ src/ # Source code
β βββ main.py # FastAPI application entry point
β βββ config.py # Configuration loader (INI files)
β βββ dependencies.py # Global dependencies
β βββ schemas.py # Shared Pydantic schemas
β βββ configuration/
β β βββ app.py # FastAPI app initialization
β βββ middlewares/ # HTTP middlewares
β β βββ __init__.py
β β βββ database.py # Database session middleware
β βββ routers/ # API routers
β β βββ __init__.py # Router registration
β β βββ root/ # Root endpoints
β β βββ router.py # Route definitions
β β βββ actions.py # Business logic
β β βββ dal.py # Data access layer
β β βββ models.py # Database models
β β βββ schemas.py # Request/response schemas
β βββ database/ # Database configuration
β β βββ core.py # Database engine and sessions
β β βββ base.py # Base model class
β β βββ dependencies.py # Database dependencies
β β βββ logging.py # Session tracking
β β βββ alembic/ # Database migrations
β βββ redis_client/ # Redis operations
β β βββ redis.py # Redis controller with caching methods
β βββ services/ # External service integrations
β βββ misc/ # Utilities
β βββ security.py # Security utilities
β βββ timezone.py # Timezone utilities
βββ docker/
β βββ Dockerfile # Production Dockerfile
β βββ Dockerfile.dev # Development Dockerfile
β βββ docker-compose.yml # Production stack
β βββ docker-compose.dev.yml # Development stack
β βββ nginx/
β βββ nginx.conf # Nginx configuration
βββ daemon-service/
β βββ fastapi-app.service # Systemd service
βββ config.ini.example # Configuration template
βββ alembic.ini.example # Alembic configuration template
βββ requirements.txt # Python dependencies
βββ Makefile # Build commands
βββ start.sh # Startup script
βββ README.md # This file
make help # Show all available commands
make install # Install dependencies
make dev # Start development environment
make build # Build production Docker image
make up # Start production environment
make down # Stop all containers
make logs # Show logs
make clean # Remove containers and volumes
make test # Run tests
make lint # Run linter
make format # Format code
make migrate # Apply migrations
make migrate-create # Create new migrationGitHub Actions workflow automatically:
- Runs tests on every PR
- Checks code with linter
- Builds Docker image
- Deploys to production on push to main
SSH_PRIVATE_KEY- SSH key for server accessSSH_HOST- Server hostSSH_USER- Server userCONFIG_INI- Contents of config.ini file for productionALEMBIC_INI- Contents of alembic.ini file for productionDOCKER_USERNAME- Docker Hub username (optional)DOCKER_PASSWORD- Docker Hub password (optional)
Application uses INI files for configuration (see config.ini.example):
[POSTGRES]
# PostgreSQL database configuration
DATABASE = postgresql
DRIVER = asyncpg
DATABASE_NAME = your_database_name
USERNAME = postgres
PASSWORD = your_password
IP = localhost
PORT = 5432
# Connection pool settings
DATABASE_ENGINE_POOL_TIMEOUT = 30
DATABASE_ENGINE_POOL_RECYCLE = 3600
DATABASE_ENGINE_POOL_SIZE = 5
DATABASE_ENGINE_MAX_OVERFLOW = 10
DATABASE_ENGINE_POOL_PING = true
# Database echo (SQL logging) - set to false in production
DATABASE_ECHO = false
[UVICORN]
# Uvicorn server configuration
HOST = 0.0.0.0
PORT = 8000
WORKERS = 4
LOOP = uvloop # Event loop: asyncio | uvloop (uvloop is faster)
HTTP = httptools # HTTP protocol: h11 | httptools (httptools is faster)
[REDIS]
# Redis cache configuration
HOST = localhost
PORT = 6379
DB = 0
PASSWORD =- Automatic session management per request
- Auto-commit on success, rollback on error
- Session tracking for debugging
- Request ID generation for tracing
- Simple caching interface with
get(),set(),delete(),update() - JSON serialization support with
get_json()andset_json() - TTL (Time To Live) management
- Multiple key deletion support
- Database connectivity check
- Redis connectivity check
- Returns 200 (healthy) or 503 (unhealthy)
- Accessible at
/api/root/health
To run as systemd service:
- Copy service file:
sudo cp daemon-service/fastapi-app.service /etc/systemd/system/-
Edit paths in service file
-
Start service:
sudo systemctl daemon-reload
sudo systemctl enable fastapi-app
sudo systemctl start fastapi-app# Run all tests
make test
# Run with coverage
pytest --cov=app --cov-report=html
# Run specific test
pytest tests/test_api.py -vmake migrate-create
# or
alembic revision --autogenerate -m "migration description"make migrate
# or
alembic upgrade headmake formatMIT License - see LICENSE file