An enterprise Python FastAPI template application to create and deploy FastAPI project.
- Python 3.11+ support
- SQLAlchemy 2.0+ support
- Asynchronous capabilities
- Database migrations using Alembic
- Basic Authentication using JWT
- Caching using Redis
- Dockerized application
- Asynchronous background tasks using Celery
- Feature flagging to enable/disable features
- Readily available CRUD operations
- Readily available middlewares for rate limiting, request id injection etc
- Type checking using mypy
- Linting using flake8
- Formatting using black
- Code quality analysis using SonarQube
- Load-tests using Locust
- Python 3
- Docker
- PostgreSQL
- To initialize and set up your environment, run the following script:
./scripts/initialize-env.shThis script installs the necessary dependencies and prepares the environment for running the FastAPI application on your machine.
- To activate the python environment, run the following command:
# Mac & Linux:
source ./venv/bin/activate
# Windows
.\venv\scripts\activate
Update database environment variables in your .env.local file:
DB_NAME=
DB_HOSTNAME=localhost
DB_PORT=3306
DB_USERNAME=
DB_PASSWORD=
Additional Configuration (Optional):
SLACK_WEBHOOK_URL=
DB_ROOT_PASSWORD=
Create new database migrations when you make changes to your models. Use the following command:
alembic revision -m 'brief description of changes'This command initializes a new migration script based on the changes made to your models. Provide a brief description of the changes in the migration message.
Apply the database migrations with the following command:
alembic upgrade headThis command updates the database schema to reflect the latest changes defined in the migration scripts
To install and set-up Redis execute the following commands:
-
brew install redis brew services start redis
-
Please refer: https://developer.redis.com/create/windows/ -
sudo apt install redis sudo systemctl enable redis sudo systemctl start redis sudo systemctl status redis # verify status
docker run --name recorder-redis -p 6379:6379 -d redis:alpineTo initialize the celery worker execute the following command:
celery -A app.app.celery worker -l info[Optional] To activate Celery Flower execute the following command:
flower --broker=${REDIS_URL}/6 --port=5555./scripts/local_server.shThis script upgrades the database migrations using Alembic and starts the FastAPI server using Uvicorn. The server is hosted on 0.0.0.0 and port 8000, making it accessible locally.
- Create a file .env.docker with reference of .env.example
- Inject Docker environment using
set -a source .env.docker set +a
- Execute following command to turn on the application
docker compose --env-file .env.docker up
Using the Circuit Breaker for External API Calls
Our application uses a circuit breaker pattern to enhance its resilience against failures in external services. The circuit breaker prevents the application from performing operations that are likely to fail, allowing it to continue operating with degraded functionality instead of complete failure.
How to Use?
- For any external service call, wrap the call with the circuit breaker.
- The circuit breaker is configured to trip after a certain number of consecutive failures. Once tripped, it will prevent further calls to the external service for a defined period.
Example
Here's an example of using the circuit breaker in an API route:
@app.get("/external-service")
async def external_service_endpoint():
try:
with circuit_breaker:
result = await external_service_call()
return {"message": result}
except CircuitBreakerError:
raise HTTPException(status_code=503, detail="Service temporarily unavailable")
To deploy the FastAPI application on AWS ECS, use the following script:
./scripts/setup-ecs.sh developThe setup-ecs.sh script leverages AWS Copilot to deploy the service. Provide the environment name as an argument (e.g., develop). The script creates and deploys an environment, then deploys the FastAPI service on that environment.
Note: Ensure you have AWS credentials configured and AWS Copilot installed for successful deployment.
If you are new to AWS Copilot or you want to learn more about AWS Copilot, please refer to this helpful article that guides you through the process of setting up AWS Copilot locally as well as also helps you understand how you can publish and update an application using 4 simple steps.
- Percona: https://localhost:443
- Flower: http://localhost:5556
- Locust UI: http://localhost:8089
- Swagger UI: http://localhost:8000
- Tests - scripts/run_tests.sh
- Linting & Formatting - scripts/lint_and_format.sh
- Load tests - scripts/load_tests.sh (Change locust.conf accordingly)
- Database Monitoring.
- Observability, Logging and Tracing (New Relic or Prometheus/Grafana).
- AWS Copilot for CD.
- Sonarqube for CI.
- DB environment variable needs to be set to the correct value.
2024-12-07 00:07:38 2024-12-06 18:37:38.367 UTC [541] FATAL: role "root" does not exist - Flower does not work on ARM64.
replace mher/flower:0.9.7 with custom image.