Note: This is a work-in-progress template. It demonstrates the architectural patterns but is not production-ready. Known gaps: no Alembic migration for the
accounttable, no integration tests,ExternalServiceClientuses a placeholder API format, andCreateAccountCommandhandler is not wired to any endpoint yet.
FastAPI service template built with Clean Architecture, DDD, CQRS, and the Mediator pattern.
- Python 3.11+, FastAPI, SQLAlchemy 2.0 (async), PostgreSQL
- Punq — dependency injection
- Structlog — structured logging
- pytest + pytest-asyncio — testing
src/
├── domain/ # Business logic — entities, value objects, domain exceptions
├── application/ # Use cases — commands, queries, DTOs, interfaces
├── infrastructure/ # Framework code — DB, DI container, mediator, external clients
└── presentation/ # HTTP layer — FastAPI routes and schemas
Layers depend inward only: presentation → application → domain.
Infrastructure implements interfaces defined in the application layer.
cp .env.example .env
docker compose -f deploy/compose/postgres.yaml up -d
poetry install
poetry run uvicorn presentation.api.main:app --host 0.0.0.0 --port 8000API docs: http://localhost:8000/api/docs
poetry install --with dev
pytest tests/ -v- Rename
accountto your domain (e.g.user,order,product) acrossdomain/,application/,infrastructure/, andpresentation/ - Replace
ExternalServiceClientwith your own external service implementation - Update
EXTERNAL_SERVICE_URLin.env - Create a new Alembic migration for your table
| Method | Path | Description |
|---|---|---|
GET |
/account/list |
List all accounts (paginated) |
POST |
/account/ |
Fetch account info from external service |
GET |
/healthcheck |
Health check |