Loading...
Loading...
Expert guidance for building production-ready FastAPI applications with modular architecture where each business domain is an independent module with own routes, models, schemas, services, cache, and migrations. Uses UV + pyproject.toml for modern Python dependency management, project name subdirectory for clean workspace organization, structlog (JSON+colored logging), pydantic-settings configuration, auto-discovery module loader, async SQLAlchemy with PostgreSQL, per-module Alembic migrations, Redis/memory cache with module-specific namespaces, central httpx client, OpenTelemetry/Prometheus observability, conversation ID tracking (X-Conversation-ID header+cookie), conditional Keycloak/app-based RBAC authentication, DDD/clean code principles, and automation scripts for rapid module development. Use when user requests FastAPI project setup, modular architecture, independent module development, microservice architecture, async database operations, caching strategies, logging patterns, configuration management, authentication systems, observability implementation, or enterprise Python web services. Supports max 3-4 route nesting depth, cache invalidation patterns, inter-module communication via service layer, and comprehensive error handling workflows.
npx skill4agent add fzozyurt/agentskills fastapi-enterprise# Create modular FastAPI project with UV
python scripts/init_project.py --name my_api --auth keycloak --with-example-module
# Result:
# my_api/ ← Project name subdirectory
# ├── src/
# │ ├── app.py ← Main application
# │ ├── core/ ← Shared infrastructure (logging, DB, cache, module loader)
# │ ├── middleware/ ← Conversation tracking, auth
# │ └── routes/ ← Core routes (health, metrics)
# ├── modules/ ← Independent modules
# │ └── users/ ← Example: own routes, models, cache, migrations
# ├── pyproject.toml ← UV configuration
# ├── config/ ← YAML configurations
# └── scripts/ ← Automation (create_module.py)
# Install and run
cd my_api
uv sync # Install dependencies (10-100x faster than pip)
cp .env.example .env
uv run uvicorn src.app:app --reload# Generate new module with complete structure
uv run python scripts/create_module.py --name orders
# Auto-creates:
# modules/orders/
# ├── __init__.py ← Router export (auto-discovered)
# ├── routes/ ← /api/v1/orders endpoints
# ├── models/ ← SQLAlchemy models (own tables)
# ├── schemas/ ← Pydantic validation
# ├── services/ ← Business logic
# ├── cache/ ← Module-specific cache (orders:* prefix)
# ├── enums/ ← Module-specific enums
# └── alembic/ ← Module-specific migrations# src/app.py
from fastapi import FastAPI
from src.core.config import settings
from src.core.logging import configure_logging
from src.core.cache import cache_manager
from src.core.module_loader import discover_modules
from src.middleware.conversation_middleware import ConversationMiddleware
# Configure logging (JSON/colored)
configure_logging(environment=settings.ENVIRONMENT)
app = FastAPI(title=settings.PROJECT_NAME)
app.add_middleware(ConversationMiddleware) # UUID tracking
@app.on_event("startup")
async def startup():
await cache_manager.connect() # Redis or memory
discover_modules(app) # Auto-register all modules
@app.get("/")
async def root():
return {"message": "Welcome"}modules/modules.users.services.UserServicefrom modules.users.models import Usermy_api/uv export --format requirements-txt > requirements.txtusers:123orders:456@cached(prefix="products", ttl=1800)users:*ConversationMiddlewareconfig/development.ymlconfig/production.yml${DATABASE_PASSWORD}asyncpgsrc/core/db.pymodules/users/alembic/modules/orders/alembic/alembic revision --autogeneratepython scripts/run_migrations.py --allX-Conversation-IDconfig/*.yml/metrics/healthrequest.state.current_user@requires_role("admin")python scripts/init_project.py --name my_api --auth keycloak --with-example-moduleuv run python scripts/create_module.py --name productsuv run python scripts/create_endpoint.py --module users --service auth --method post --path loginuv run python scripts/create_model.py --module products --name Productuv run python scripts/validate_structure.pyfrom modules.users.services.user_service import UserServiceusers:cache:123orders:cache:456from modules.users.models.user import UserForeignKey("users.id")scripts/init_project.pyscripts/create_module.pyscripts/create_endpoint.pyscripts/create_model.pyscripts/validate_structure.py