首页/ AI 技能 / @skilllib/skill-2117939930272414473
Skill站外收录 @skilllib/skill-2117939930272414473

fastapi-router-creator

提供 FastAPI 路由的模块化组织与自动化构建能力,支持按功能拆分路由文件并统一聚合,兼顾版本隔离、依赖注入和 OpenAPI 文档标签管理,适用于中大型 API 项目的可维护性架构设计。

0.0 (0) 31 次安装 原作者 编程导航
收录于 2026-01-22
软件开发 AI 技能

Skills.MD

# FastAPI Router Creator This skill guides the creation of modular, organized FastAPI routers, emphasizing maintainability and scalability. ## Routing Strategies ### 1. Modular Router Pattern (Standard) The most common and recommended approach for FastAPI. **Structure:** ``` src/api/v1/endpoints/ ├── users.py ├── items.py └── auth.py ``` **Implementation:** `src/api/v1/endpoints/users.py`: ```python from fastapi import APIRouter router = APIRouter() @router.get("/") async def get_users(): ... ``` `src/api/v1/api.py` (Aggregator): ```python from fastapi import APIRouter from src.api.v1.endpoints import users, items api_router = APIRouter() api_router.include_router(users.router, prefix="/users", tags=["users"]) api_router.include_router(items.router, prefix="/items", tags=["items"]) ``` ### 2. File-Based Routing (fastapi-router) For a Next.js-like experience where file structure dictates URLs. (Requires `fastapi-router` library or custom walker). **Structure:** ``` src/app/ ├── api/ │ ├── users/ │ │ ├── route.py # Handles /api/users │ │ └── [id]/ │ │ └── route.py # Handles /api/users/{id} ``` ## Best Practices 1. **Prefixing**: Use prefixes at the router include level, not inside every endpoint decorator. 2. **Tags**: Use tags to group endpoints in OpenAPI docs. 3. **Dependencies**: Apply dependencies (like auth) at the router level if they apply to all endpoints in that router. ```python router = APIRouter(dependencies=[Depends(get_current_active_user)]) ``` 4. **Version**: Namespace routers by API version (`v1`, `v2`).

评论 0

还没有评论,来抢沙发。