一种基于 Uncle Bob 书中的 Clean Architecture 实现
A Python architecture sample that demonstrates Uncle Bob's Clean Architecture through a Pokémon and Trainer API built with FastAPI. The focus is on showing how layers are separated, how dependencies flow inward, and how infrastructure can be swapped at the structural level.
This is a learning-oriented project, not a production template. For background and common questions, see the FAQ.
This project applies Clean Architecture with the following practices:
*source: yoan-thirion.gitbook.io
…REST and GraphQL share the same use cases layer — the API protocol is an outer-layer detail. 5 databases (SQLite, MySQL, PostgreSQL, MongoDB, Redis) sit behind the same repository contracts — storage is also an outer-layer detail.
Two entities illustrate different complexity levels:
Enabled by several key design decisions:
The flow diagrams visualize the layers and how they interact:
For a detailed explanation of the ASCII flow, refer to ascii-flow.md.
*source: yoan-thirion.gitbook.io
*source: https://stackoverflow.com/a/73788685
Start the application with a single command — no local Python or dependency installation required:
$ docker compose up appUses in-memory SQLite by default. Access the API documentation at http://localhost:8000/docs and try the interactive endpoints.
Set DATABASE_URI to switch databases:
| Database | URI format |
|---|---|
| SQLite (file) | sqlite+aiosqlite:///.db |
| SQLite (memory) | sqlite+aiosqlite:///:memory: (default) |
| MySQL | mysql+asyncmy://:@:/ |
| PostgreSQL | postgresql+asyncpg://:@:/ |
| MongoDB | mongodb://:@:/ |
| Redis | redis://:@:/ |
Note: If you encounter database initialization issues, append
reinitialize=trueto theDATABASE_URI, e.g.,sqlite+aiosqlite:///sqlite.db?reinitialize=true.
$ docker compose down --remove-orphans -v
$ docker compose up dockerizeFor local development with hot-reload:
Install prerequisites: Python 3.11+ and uv
Install dependencies: [^5]
$ uv syncLaunch the application:
# With default SQLite database
$ make up
# With specific database
$ DATABASE_URI= make upAccess the application: http://localhost:8000
# Test against a specific database
$ DATABASE_URI= uv run pytest
# Test against all databases (requires bats)
$ make testFor supported database URIs, see Database Options
Note: Use a different
dbnamewith "_test" suffix for testing (e.g., "mydatabase_test") to avoid interfering with your main application data.
See the full version history in CHANGELOG.md.
If this project helped you, a ⭐ would be greatly appreciated!
Have questions or ideas? Feel free to open an issue or submit a PR.
[^5]: The uv sync command installs all required packages for running and developing the application. However, it does not include cspell. If you need cspell for spell checking, please refer to the official installation guide at cspell installation guide
暂无开放 Issues,或尚未同步最近议题。