How jdglaser/fastapi-example-project works
Main Components
• API Server: The core FastAPI application hosted in src/main.py.
• Routes: Organized under src/routes (with separate routers for authentication, user, and item operations).
• Services: Located in src/services (e.g., auth_service.py and item_service.py) that contain business logic.
• Models: Defined under src/models, representing data schemas for items, users, tokens, etc.
• Database Layer: This is split across several folders:
• Database connection and setup (src/database/database.py)
• Repositories that encapsulate CRUD operations (src/database/repos)
• Database tables definitions (src/database/tables)
• Migration Infrastructure: Utilizes Alembic for database schema migrations (src/alembic and alembic.ini).
• Utility Functions: Helpers and logging utilities are in src/util.
• Tests: Full test coverage is provided in the tests folder.
• Supporting Infrastructure: Docker (Dockerfile, docker-compose.yml) and Pipenv for dependency management, along with shell scripts for migrations and app/database startup.
Relationships and Interactions
• Client calls the FastAPI endpoints exposed via routes (auth_router, item_router, user_router).
• FastAPI routes invoke services (e.g., auth_service, item_service) to perform business logic.
• Services interact with database repositories to perform data operations.
• The database repositories use underlying table definitions for ORM-level database transactions.
• Alembic scripts (migrations) maintain the evolution of database schema changes.
• Docker and docker-compose manage deployment, linking containerized services (the FastAPI app and the database).
Architectural Patterns and Design Principles
• Separation of Concerns: Clear division between routes (handling HTTP requests), service layer (business logic), repository layer (database abstraction), and utility functions.
• Layered Architecture: Presentation (API routes), business logic (services), and data access (repositories and database tables).
• Use of Dependency Injection, error handling, logging, and modular tests to enhance maintainability.
• Containerization: Utilizes Docker for creating isolated environments for both the application and the database.
Relevant Technologies and Frameworks
• FastAPI: For asynchronous API serving.
• Uvicorn: ASGI server used to run the FastAPI app.
• Docker & Docker Compose: For containerization and running services.
• Alembic: For managing database migrations.
• Pipenv: For dependency management.
• Testing Frameworks: Likely Pytest, given the structure in the tests directory.
a. Client/User interacting with the “FastAPI Example Project” via HTTP requests.
• “Routes” (showing auth_router, item_router, user_router)
• “Services” (auth_service, item_service)
• “Models” (item_models, user_models, token_models)
e. A side branch for “Infrastructure & Deployment”:
• Docker files (Dockerfile, docker-compose.yml)
• Shell scripts for starting the app and database, migration generation, and cleanup.
• Dependency Management via Pipenv.
f. Lastly, point out “Documentation” (docs folder) and “Tests” (tests folder) as complementary parts of the overall project structure.