AgentSkillsCN

fastapi-enterprise

专家级指导,助您构建具备模块化架构的生产级 FastAPI 应用——每个业务领域都独立为一个模块,拥有各自的路由、模型、数据结构、服务、缓存与迁移方案。采用 UV + pyproject.toml 实现现代化的 Python 依赖管理,以项目名称作为子目录,实现工作空间的清晰组织;搭配 structlog(JSON+彩色日志)、pydantic-settings 配置管理、模块自动发现加载器、结合 PostgreSQL 的异步 SQLAlchemy、按模块划分的 Alembic 迁移方案、带有模块专属命名空间的 Redis/内存缓存、中央化的 httpx 客户端、OpenTelemetry/Prometheus 可观测性支持、对话 ID 跟踪(X-Conversation-ID 头部+Cookie)、基于条件的 Keycloak/应用级 RBAC 认证、DDD 与清洁代码原则,以及用于加速模块开发的自动化脚本。适用于用户希望获得 FastAPI 项目搭建、模块化架构设计、独立模块开发、微服务架构、异步数据库操作、缓存策略、日志记录模式、配置管理、认证系统、可观测性落地,或企业级 Python Web 服务相关指导的场景。支持最多 3–4 层路由嵌套深度,提供缓存失效模式、通过服务层实现模块间通信,以及全面完善的错误处理流程。

SKILL.md
--- frontmatter
name: fastapi-enterprise
description: 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.
license: Complete terms in LICENSE.txt

FastAPI Enterprise Development - Modular Architecture

Enterprise-grade FastAPI with modular architecture: each business domain is an independent module with own database tables, cache namespace, routes, and migrations. Modern Python tooling (UV + pyproject.toml) for fast dependency management.

Quick Start

Initialize Project

bash
# 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

Create Independent Module

bash
# 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

Minimal Modular App

python
# 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"}

Core Capabilities

1. Modular Architecture ⭐

  • Independent Modules: Each module owns DB tables, cache keys, routes
  • Auto-Discovery: Modules in modules/ automatically registered
  • Service-Layer Communication: Inter-module calls via services (no direct model imports)
  • Per-Module Migrations: Each module has own Alembic history
  • Enforced Boundaries: modules.users.services.UserService ✅ | from modules.users.models import User

Module Patterns Guide

2. Modern Python Tooling (UV + pyproject.toml) ⭐

  • UV: 10-100x faster than pip, Rust-based package manager
  • pyproject.toml: PEP 518/621 standard, replaces requirements.txt
  • uv.lock: Reproducible builds across environments
  • Project Subdirectory: All files in my_api/ (clean workspace)
  • Export: uv export --format requirements-txt > requirements.txt when needed

Project Structure

3. Flexible Caching ⭐

  • Redis (production): Distributed, persistent cache
  • Memory (development): Zero dependencies, instant setup
  • Module Namespaces: users:123, orders:456 (no collisions)
  • Cache Decorators: @cached(prefix="products", ttl=1800)
  • Invalidation: Time-based (TTL), event-based (on update), pattern-based (users:*)

Cache Patterns

4. Structured Logging (structlog)

  • JSON (production): Structured logs for aggregation (ELK, Splunk)
  • Colored Console (development): Human-readable with syntax highlighting
  • Conversation ID: UUID in all logs from ConversationMiddleware
  • Request/Response: Automatic logging with duration, status code
  • External APIs: Conversation ID propagated to downstream services

Logging Patterns

5. Type-Safe Configuration

  • pydantic-settings: OS environment variables with type validation
  • YAML: config/development.yml and config/production.yml
  • Variable Substitution: ${DATABASE_PASSWORD} from environment
  • CONFIG_ENV Switcher: Toggle between dev/prod configs

Configuration Patterns

6. Async Database (SQLAlchemy 2.0)

  • Async: Full asyncio support with asyncpg driver
  • PostgreSQL-Optimized: JSONB, arrays, full-text search
  • Per-Module Tables: Each module creates own tables
  • Central Session: src/core/db.py session factory used by all modules and Alembic

Database Patterns

7. Per-Module Migrations (Alembic)

  • Independent Histories: modules/users/alembic/, modules/orders/alembic/
  • Async Template: Configured for async SQLAlchemy
  • Auto-Generation: alembic revision --autogenerate
  • Run All: python scripts/run_migrations.py --all

Alembic Setup

8. Central HTTP Client (httpx)

  • Async: Connection pooling, timeouts, retries
  • Conversation ID: Auto-propagated to X-Conversation-ID header
  • YAML Configs: Base URLs from config/*.yml
  • Request Logging: All external API calls logged

HTTPx Patterns

9. Observability

  • Conversation Tracking: UUID from header/cookie, appears in all logs
  • Prometheus Metrics: /metrics endpoint (request count, latency, errors)
  • OpenTelemetry (optional): Distributed tracing (Jaeger, Zipkin)
  • Health Check: /health with DB connectivity status

Observability Patterns

10. Authentication (Conditional)

AI MUST ASK: "Keycloak authentication (JWT from external IdP) or app-based RBAC (database-backed roles)?"

  • Keycloak: JWT validation, role extraction, Keycloak OpenID integration
  • App-based: Custom JWT, database roles/permissions, user management
  • Middleware: Token validation, request.state.current_user injection
  • Role Decorators: @requires_role("admin")

Authentication Patterns

Automation Scripts

init_project.py

Initialize complete project with modular structure:

bash
python scripts/init_project.py --name my_api --auth keycloak --with-example-module

create_module.py

Generate new independent module:

bash
uv run python scripts/create_module.py --name products

create_endpoint.py

Add endpoint to existing module:

bash
uv run python scripts/create_endpoint.py --module users --service auth --method post --path login

create_model.py

Create SQLAlchemy model + Alembic migration:

bash
uv run python scripts/create_model.py --module products --name Product

validate_structure.py

Check project compliance:

bash
uv run python scripts/validate_structure.py

Module Independence Rules

✅ DO:

  • Service Layer Communication: from modules.users.services.user_service import UserService
  • Module Namespaces: users:cache:123, orders:cache:456
  • Own Tables: Each module creates own database tables
  • Own Migrations: Separate Alembic history per module

❌ DON'T:

  • Direct Model Imports: from modules.users.models.user import User (use service instead)
  • Shared Tables: No ForeignKey("users.id") across modules (store as regular int)
  • Cross-Module Cache: Don't access other module's cache directly

Clean Code Standards

Resources

Reference Documentation

Automation Scripts

  • scripts/init_project.py - Initialize complete project
  • scripts/create_module.py - Generate new module
  • scripts/create_endpoint.py - Add endpoint to module
  • scripts/create_model.py - Create model + migration
  • scripts/validate_structure.py - Check project compliance

Examples

License

MIT License - See LICENSE.txt for complete terms.