A modern, production-ready FastAPI application with PostgreSQL, Redis, Elasticsearch, and Docker support.
- FastAPI - Modern, fast web framework for building APIs
- PostgreSQL - Primary database with async support via SQLAlchemy
- Redis - Caching and session storage
- Elasticsearch - Search and analytics
- Alembic - Database migrations
- Docker - Containerized deployment
- Poetry - Dependency management
- Sentry - Error tracking and monitoring
- CORS - Cross-origin resource sharing
- Session Management - Secure session handling
- Docker & Docker Compose
- Python 3.11+ (for local development)
- Poetry (for dependency management)
-
Clone the repository
git clone <repository-url> cd fastapi-init
-
Set up environment variables
cp .env.example .env # Edit .env with your configuration -
Start the application
make start
-
Access the application
- API: http://localhost:8081
- API Documentation: http://localhost:8081/docs
- Health Check: http://localhost:8081/ping
-
Install dependencies
poetry install
-
Set up environment variables
cp .env.example .env # Configure your local database and Redis settings -
Run the application
poetry run uvicorn app.main:app --reload --host 0.0.0.0 --port 8081
Create a new migration:
make migrate "Your migration message"Apply migrations:
make migration| Command | Description |
|---|---|
make start |
Start all services |
make stop |
Stop all services |
make restart |
Restart all services |
make build |
Build Docker images |
make deploy |
Deploy to Docker Swarm |
Format code:
make formatLint code:
make lintapp/
├── alembic/ # Database migrations
├── core/ # Core functionality
│ ├── cache.py # Redis cache management
│ ├── config.py # Application settings
│ ├── db.py # Database configuration
│ ├── elastic.py # Elasticsearch setup
│ ├── logger.py # Logging configuration
│ └── middleware.py # Custom middleware
├── models/ # SQLAlchemy models
├── routers/ # API routes
├── services/ # Business logic
├── utils/ # Utility functions
└── main.py # Application entry point
The application uses environment variables for configuration. Key settings include:
PROJECT_NAME- Application nameDOMAIN- Domain for the applicationSESSION_SECRET_KEY- Secret key for sessionsENVIRONMENT- Environment (local/staging/production)- Database credentials (PostgreSQL)
- Redis configuration
- Sentry DSN (optional)
- Health Check:
/pingendpoint for service health - Process Time: Middleware adds
X-Process-Timeheader - Sentry: Automatic error tracking (in non-local environments)
- Logging: Structured logging with configurable levels
-
Initialize swarm:
make swarm-init
-
Deploy:
make deploy
- Set
ENVIRONMENT=production - Configure proper
SENTRY_DSN - Use secure
SESSION_SECRET_KEY - Set up proper database backups
- Configure reverse proxy (nginx/traefik)
Once running, access the interactive API documentation at:
- Swagger UI: http://localhost:8081/docs
- ReDoc: http://localhost:8081/redoc
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests and linting
- Submit a pull request
This project is licensed under the MIT License.
Nurdaulet Maxutov
- Email: maksutovnurda@gmail.com
For more information, check the API documentation at /docs when the application is running.