This project is a starter template for Python applications using FastAPI, Celery, SQLAlchemy, and Docker. It includes a basic API, worker, and database setup.
The Makefile provides convenient shortcuts for common tasks. Run make help to see all available targets. Key targets include:
Dependency Management (Poetry):
make install: Install Python dependencies frompoetry.lock.make lock: Updatepoetry.lockbased onpyproject.toml(no install).make update: Update Python dependencies to latest allowed versions and updatepoetry.lock.
Linting & Formatting:
make lint: Run Ruff linter and Mypy type checker.make format: Format code using Ruff formatter.
Testing:
make test: Run tests using Pytest.make test-cov: Run tests and generate a coverage report.
Running Locally (Requires nix develop):
make run-api: Start the FastAPI development server with hot-reloading.make run-worker: Start the Celery worker locally.
Database Migrations (Alembic):
make db-migrate: Generate a new migration script based on model changes (edit message!).make db-upgrade: Apply pending migrations to the database (requires Docker DB running).make db-downgrade: Revert the last migration.make db-current: Show the current migration version applied to the database.make db-history: Show the full migration history.
Docker:
make docker-build: Build the Docker images for the application services.make docker-up: Start all services defined indocker-compose.ymlin detached mode.make docker-down: Stop and remove containers, networks defined indocker-compose.yml.make docker-logs: Follow logs from running Docker containers.make docker-prune: Remove stopped containers and dangling Docker images to free up space.
Nix:
make nix-shell: Alias fornix develop.make nix-update: Update Nix flake inputs (e.g.,nixpkgs).make nix-clean: Run Nix garbage collection.
- Enter Environment: Start Docker services (
make docker-up) and enter the Nix shell (nix develop). - Code: Make changes to your application code in
src/app/. - Lint/Format: Run
make formatandmake lintperiodically to ensure code quality and type safety. Fix any reported issues. - Test: Run
make testormake test-covto ensure your changes pass tests and maintain coverage. Write new tests for new features. - Database Changes (If necessary):
- Modify SQLAlchemy models in
src/app/models/. - Generate a migration script:
make db-migrate(update the-m "..."message in the Makefile or command). - Review the generated script in
src/alembic/versions/carefully. - Apply the migration:
make db-upgrade.
- Modify SQLAlchemy models in
- Commit: Add your changes and any new migration scripts to Git (
git add .,git commit ...). - Repeat.
Application settings are managed using pydantic-settings in src/app/core/config.py.
- Settings are loaded from environment variables.
- Environment variables can be conveniently defined in a
.envfile in the project root during development. - The
.env.examplefile shows available settings. Copy it to.envand customize. - Never commit your
.envfile.
Alembic is used to manage database schema changes.
- Modify your SQLAlchemy models under
src/app/models/. - Generate a new migration script:
make db-migrate(customize the message). - Review the generated Python script in
src/alembic/versions/. This is crucial. - Apply the migration to your running database:
make db-upgrade. - Commit both your model changes and the new migration script.
While make run-api and make run-worker are useful for rapid development with hot-reloading, you can also run the entire application stack using Docker Compose, which is closer to a production environment.
make docker-build: Build fresh images.make docker-up: Start the API, worker, DB, Redis, and Flower containers.- Access the API at
http://localhost:8000. - Access Flower at
http://localhost:5555. make docker-logs: View combined logs from all services.make docker-down: Stop and remove all application containers.
Tests are located in the tests/ directory and use pytest.
- Run all tests:
make test - Run tests with coverage:
make test-cov(configuration inpyproject.toml)
Code style and quality are enforced using Ruff and Mypy.
- Check formatting, linting, and types:
make lint - Automatically format code:
make format - Configuration for these tools is in
pyproject.tomlunder[tool.ruff]and[tool.mypy].
This project is licensed under the MIT License - see the LICENSE file for details (or state MIT License directly if no separate file).