How rawheel/fastapi-boilerplate works
Project Type and Purpose
• This project is a FastAPI backend boilerplate designed for rapid setup of a production-ready CRUD application.
• It includes built-in support for database migrations (via Alembic), ORM (via SQLAlchemy), authentication, logging, and comprehensive testing (with Pytest).
• It utilizes Docker and docker-compose to provide containerized services including the FastAPI server, PostgreSQL database, and PgAdmin for database management.
File Structure
• Top-Level Components:
• “app” directory: Holds the main application code (initialization, routing, business logic, models, views, and schemas).
▸ app/main.py: The entry point for the FastAPI server.
▸ app/core: Core functionality such as authentication (auth.py), database sessions (both real and mock sessions), logging, and a central router.
▸ app/sneakers: Contains a domain-specific module (CRUD operations, models, schemas, views) demonstrating how APIs are organized.
▸ app/tests: Contains test cases, hinting at a Test-Driven Development (TDD) approach.
• “alembic” directory: For handling database migrations.
▸ Contains configuration (alembic.ini), environment setup, and version scripts for DB schema changes.
• Docker Setup:
▸ .docker and docker-compose.yml: Define the containerization of the application, Postgres, and PgAdmin.
• Other support files:
▸ README.md, pyproject.toml, and pre-commit configuration files ensure dependency management and code quality.
README for Additional Insights
• The README confirms that it’s a production-ready FastAPI backend with a structure similar to Django.
• It specifies that the solution comes ready with a docker-compose command to build up the services, and emphasizes integrations with PostgreSQL and PgAdmin.
• It outlines usage of specific libraries such as Alembic (for DB migrations), SQLAlchemy (for ORM), Pydantic (for data validation), and Loguru (for logging).
a. Main Components:
• API Server (FastAPI Application): Represented by “app/main.py” and components in “app/core” and “app/sneakers”. This is the central processing unit for client requests.
• Database:
▸ PostgreSQL: The main production database container.
▸ PgAdmin: A tool/service for managing the PostgreSQL database.
• Migration Service:
▸ Alembic: Manages database schema migrations.
• Containerization and Deployment:
▸ Docker & docker-compose: Wrap the different services (FastAPI app, PostgreSQL, PgAdmin) into isolated containers.
• Testing Framework and Environment:
▸ Test modules in “app/tests” indicate unit and possibly integration tests.
• Utility and Configuration:
▸ Configuration files (e.g., Dockerfile, .dockerignore, pyproject.toml) and pre-commit hooks ensure development quality.
b. Relationships and Interactions:
• The client (e.g., via Swagger UI on localhost:8000/docs) sends API requests that are processed by the FastAPI server.
• Business logic and data interactions are handled through defined routers and view functions found within the “app” modules.
• The FastAPI application interacts with the PostgreSQL database using SQLAlchemy sessions (real sessions in production; mock sessions for testing).
• Alembic ensures that any database schema changes are properly versioned and applied.
• Logging (via Loguru) provides central logging functionality that can be visualized as part of the requests’ lifecycle.
• Docker and docker-compose orchestrate the deployment and networking between these components.
c. Architectural Patterns and Design Principles:
• Clear separation of concerns:
▸ “app/core” handles application-wide services (authentication, logging, DB sessions).
▸ “app/sneakers” is a feature-specific module for CRUD operations, mirroring Django-like organization.
• Containerization for reproducibility and isolation.
• Testing as a first-class concept with separate configurations for production (PostgreSQL) vs. testing (SQLite or mock sessions).
• Use of industry-standard libraries (e.g., SQLAlchemy, Pydantic) follows best practices for ORM and data validation.
d. Relevant Technologies:
• FastAPI as the web framework.
• Docker and docker-compose for containerization.
• PostgreSQL and PgAdmin for database management.
• Alembic for handling database migrations.
• SQLAlchemy for ORM, with testing configurations for SQLite or mock sessions.
• Pytest for testing.
• Poetry for dependency management.
• Loguru for log handling.
Essential Elements
▸ Client ⇄ FastAPI Server (with authentication and API routing)
▸ FastAPI Server → Database (via SQLAlchemy sessions)
▸ Alembic → Database (for schema migrations)
▸ FastAPI Server → Logging (via Loguru)
▸ Application logic (e.g., business modules in “app/sneakers” and “app/core”)
▸ Infrastructure services (e.g., PostgreSQL, PgAdmin, Docker)
▸ Development aspects (e.g., testing and mock sessions)