How amrit2356/template-fastapi-rest works
Project Type & Purpose
• This is a production-ready, back-end-only FastAPI REST template built in Python (3.11+) using Clean Architecture principles.
• It provides a modular API layer (8 endpoints), enterprise-grade configuration, process & server management, structured logging, multiple authentication methods, file management, plugin support, and built-in monitoring.
Main Components
a. Entry Point
• app.py – boots the FastAPI app, wires in middleware, routers, managers.
b. API Layer (src/api)
• routes.py – defines HTTP endpoints (/api/v1/…).
• handlers.py – request handling logic, session tracking.
• models.py – Pydantic models for request/response schemas.
c. Authentication Module (src/auth)
• auth_factory.py – selects auth strategy (JWT, API-Key, Hybrid) based on config.
• middleware.py – intercepts requests, enforces security.
• handlers/ – individual implementations for JWT, API-Key, Hybrid.
• models.py – auth data structures.
d. Core Managers (src/core)
• server_manager.py – application lifecycle (startup, shutdown, housekeeping).
• process_manager.py – orchestrates stateful/stateless processes, performs cleanup.
• managers.py – utility functions to fetch manager instances from request context.
e. Business Services (src/services)
• services.py – generic service implementations and pipeline stubs.
• pipelines/ – placeholder for custom processing pipelines.
f. Utilities (src/utils)
• config/ – YAML + env-var overrides, validation, modular loaders/providers.
• io/ – file reader/writer abstractions.
• resources/ – logger configuration, resource helpers/downloader.
g. Runtime Storage (runtime/)
• temp, outputs, logs, models, uploads, assets – file system areas managed by server/process managers.
h. Plugins (src/plugins)
• empty directory – extension point for database, monitoring, cloud providers.
i. Tests & Docs
• tests/ – unit, integration, e2e.
• docs/ – security and architecture documentation.
Relationships & Data Flow
1. Client → HTTP → Uvicorn/FastAPI (app.py)
2. Middleware Chain:
– CORS & Security (src/auth/middleware.py)
– Logging & Metrics
3. Router Dispatch (src/api/routes.py) → Handler (src/api/handlers.py)
4. Handlers call → Core Managers (e.g., get_server_manager, get_process_manager)
5. Core Managers interact with → Services (src/services) and → Utilities (config, io, resources)
6. Services may launch background processes via Process Manager
7. Process Manager writes to runtime/ (temp, outputs) and tracks cleanup
8. Server Manager drives graceful startup/shutdown and housekeeping tasks
9. External calls (e.g., OpenAI, Google APIs) orchestrated by service code using API keys from config
Architectural Patterns & Principles
• Clean Architecture: clear separation between API, domain (services), infrastructure (utils), and orchestration (core).
• Dependency Injection: managers and settings injected into handlers and services.
• Plugin Architecture: src/plugins illustrates extension points.
• Strategy Pattern: auth_factory chooses auth handler.
• Pipeline Pattern: placeholder pipelines for customizable processing.
• Configuration as Code: YAML + env-vars, modular loaders, validators.