How carlosmmorera/fastapi-ms-base works
Project Type & Purpose
• This is a microservice designed using Python and FastAPI.
• Its purpose is to offer a base template for building FastAPI microservices.
• It includes API endpoints, middleware, configuration management, and a storage layer (with PostgreSQL as an example connector).
Analyzing the File Structure
• Top-level files (.env, .gitignore, Dockerfile, README.md) indicate environment configuration and containerization.
• The config/ directory with JSON files (default, dev, and pro configurations) suggests environment-specific configuration management.
• The src/ folder is the heart of the application and contains:
• The main FastAPI application file (app.py and main.py)
• An api/ subdirectory that holds several important elements:
• controllers (e.g., health.py): for business logic tied to API endpoints.
• exceptions: where custom HTTP exceptions are defined.
• middleware (mdw): which includes authentication checks and logging.
• routers: to establish paths for configuration and example API endpoints.
• schema: for request/response validation using Pydantic models.
• A config/ subdirectory inside src for application-specific configuration files (e.g., postgresql settings, API versioning).
• A storage/ subdirectory responsible for data persistence, including connectors for PostgreSQL, CRUD operations, and entity definitions.
• Additional support files like the Postman collection imply integrated API testing.
Key Components
a. Front-facing API Layer:
• Subdivide into Routers and Controllers:
• Routers handle endpoint definitions (src/api/routers).
• Controllers process business logic (src/api/controllers).
• Middleware components:
• Authentication and logging middleware (src/api/mdw) that pre-process requests.
• Exception handling (src/api/exceptions) to standardize error responses.
• API Schema:
• Data validation and serialization (src/api/schema).
b. Configuration Management:
• A configuration module reading from config/ and envs/ directories.
• This allows dynamic configuration based on environments (development, production, etc.).
c. Storage Layer:
• Database connectivity shown via a PostgreSQL connector (src/storage/connectors/postgresql.py).
• CRUD operations and entity models in src/storage/crud and src/storage/entities.
d. External & Development Tools:
• Dockerfile (containerization).
• Postman collection for API testing.
• Uvicorn as the ASGI server running the application.
Architectural Patterns & Relationships
• The separation of concerns is clear: API (presentation) → Controllers (logic) → Storage (data).
• The project adopts a layered architecture:
• The API layer communicates with middleware (e.g., authcheck, logging) before dispatching to controllers.
• Controllers interact with a storage layer that abstracts database operations.
• Configuration and environment management is isolated from the core business logic, ensuring flexibility.