$npx -y skills add youlaitech/youlai-skills --skill fastapiFastAPI 后端开发规范. Use this skill when developing FastAPI projects, implementing REST APIs with SQLAlchemy async, Pydantic v2 schemas, JWT/Redis token authentication, or the youlai-fastapi admin backend.
| 1 | # FastAPI 后端开发规范 |
| 2 | |
| 3 | ## 触发条件 |
| 4 | |
| 5 | - Develop FastAPI projects |
| 6 | - Implement async REST APIs |
| 7 | - Use SQLAlchemy 2.0 async ORM |
| 8 | - Use Pydantic v2 for data validation |
| 9 | - Implement JWT / Redis Token authentication |
| 10 | - Implement role-based permission control |
| 11 | - Develop youlai-fastapi admin backend modules |
| 12 | |
| 13 | --- |
| 14 | |
| 15 | ## Part 1: 技术栈 |
| 16 | |
| 17 | | 层 | 选型 | 说明 | |
| 18 | |---|------|------| |
| 19 | | Web 框架 | **FastAPI** 0.115+ | 异步原生、Pydantic v2 集成、自动 OpenAPI 文档 | |
| 20 | | ASGI 服务器 | **Uvicorn** 0.30+ | FastAPI 官方推荐 | |
| 21 | | ORM | **SQLAlchemy 2.0** + **asyncpg** | `Mapped` + `mapped_column` 声明式、完整 async 支持 | |
| 22 | | 数据验证 | **Pydantic v2** | Rust 核心、FastAPI 原生集成 | |
| 23 | | 数据库 | **PostgreSQL** 16+ | 生产级、asyncpg 异步驱动 | |
| 24 | | 缓存 | **Redis** 7.x | `redis[hiredis]` 异步客户端 | |
| 25 | | 认证 | **PyJWT** 2.x + **bcrypt** 4.x | JWT 签发/验签、密码哈希 | |
| 26 | | 日志 | **loguru** 0.7+ | 零配置、彩色输出、自动 rotate | |
| 27 | | 包管理 | **uv** | Rust 编写、10-100x pip 速度 | |
| 28 | | 代码质量 | **ruff** + **mypy** | Lint + 格式化 + 静态类型检查 | |
| 29 | | 测试 | **pytest** + **httpx** | httpx AsyncClient 直测 FastAPI | |
| 30 | |
| 31 | --- |
| 32 | |
| 33 | ## Part 2: 目录结构 |
| 34 | |
| 35 | > 目录结构以社区标杆 [zhanymkanov/fastapi-best-practices](https://github.com/zhanymkanov/fastapi-best-practices) 为准。 |
| 36 | |
| 37 | ``` |
| 38 | youlai-fastapi/ |
| 39 | ├── app/ |
| 40 | │ ├── __init__.py |
| 41 | │ ├── main.py # FastAPI 入口:挂载路由、中间件、异常处理器 |
| 42 | │ ├── config.py # Pydantic Settings |
| 43 | │ ├── database.py # 异步引擎 + session + get_db |
| 44 | │ ├── redis.py # Redis 连接池 + get_redis |
| 45 | │ ├── response.py # Result[T] + ResultCode 统一响应 |
| 46 | │ ├── exceptions.py # BusinessException + 全局异常处理器 |
| 47 | │ ├── enums.py # 全局枚举 |
| 48 | │ ├── pagination.py # PageQuery / PageResult |
| 49 | │ ├── constants.py # 全局常量:安全前缀 / REDIS_ 前缀 / 系统级 |
| 50 | │ ├── dependencies.py # 全局依赖 get_current_user / require_perm |
| 51 | │ ├── middleware.py # CORS + 请求日志 + 限流 |
| 52 | │ ├── registry.py # 导入全部域模型,触发 Base.metadata 注册(Base/Mixin 在 database.py) |
| 53 | │ ├── auth/ # 认证域:router.py / schemas.py / service.py / token.py / utils.py |
| 54 | │ ├── captcha/ # 验证码:service.py / constants.py |
| 55 | │ ├── system/ # 系统管理域 |
| 56 | │ │ ├── user/ role/ menu/ dept/ dict/ log/ |
| 57 | │ │ │ └── 各含 router.py schemas.py service.py models.py |
| 58 | │ │ │ (role/ 含 constants.py、data_permission.py;log/ 含 constants.py、operation_log.py) |
| 59 | │ │ ├── config/ # 仅 models.py + router.py(配置内联,无 schemas/service) |
| 60 | │ │ └── notice/ # 仅 models.py + router.py |
| 61 | │ └── tool/ # 工具与集成域 |
| 62 | │ └── file/ codegen/ wxma/ sse/ # 文件 / 代码生成 / 微信小程序 / 服务端推送 |
| 63 | ├── alembic/ # 数据库迁移 |
| 64 | ├── alembic.ini |
| 65 | ├── tests/ |
| 66 | │ ├── conftest.py |
| 67 | │ ├── auth/test_auth.py # 按业务域镜像 |
| 68 | │ └── test_result_code.py # 全局 |
| 69 | ├── Dockerfile |
| 70 | ├── docker-compose.yml |
| 71 | ├── pyproject.toml # uv 依赖管理 |
| 72 | ├── .env.example |
| 73 | ├── .gitignore |
| 74 | └── README.md |
| 75 | ``` |
| 76 | |
| 77 | **设计原则**: |
| 78 | - 按业务域组织:每个域目录自包含 `router/schemas/models/service`(按需含 `constants/dependencies/exceptions`),域间通过显式模块名导入。 |
| 79 | - `models/` 仅放 ORM 基类与全域注册,纯 ORM 不依赖任何业务层。 |
| 80 | - 全局基础设施(config / database / redis / response / exceptions / pagination / constants / dependencies / middleware)平铺在 `app/` 根,被各域共享。 |
| 81 | - 不设 `core/` `common/` `framework/` `modules/` 分层包。 |
| 82 | |
| 83 | --- |
| 84 | |
| 85 | ## Part 3: 命名规范 |
| 86 | |
| 87 | ### 3.1 文件命名 |
| 88 | |
| 89 | | 类型 | 规范 | 示例 | |
| 90 | |------|------|------| |
| 91 | | 模块目录 | 小写单数名词 | `user/`, `role/`, `menu/` | |
| 92 | | Schema 文件 | `schemas.py` | `app/system/user/schemas.py` | |
| 93 | | Service 文件 | `service.py` | `app/auth/service.py` | |
| 94 | | Router 文件 | `router.py` | `app/system/role/router.py` | |
| 95 | | 配置文件 | `config.py` | `app/config.py` | |
| 96 | | 模型文件 | 按域拆分 | `app/system/user/models.py` | |
| 97 | | 常量文件 | `constants.py` | `app/constants.py`(全局)/ `app/system/user/constants.py`(域) | |
| 98 | | 枚举文件 | `enums.py` / `constants.py` | `app/enums.py`(全局)/ `app/system/role/constants.py`(域枚举) | |
| 99 | | 测试文件 | `test_<模块>.py` | `tests/auth/test_auth.py` | |
| 100 | |
| 101 | ### 3.2 类命名 |
| 102 | |
| 103 | | 类型 | 规范 | 示例 | |
| 104 | |------|------|------| |
| 105 | | ORM 模型 | `Sys` + PascalCase | `SysUser`, `SysRole`, `SysMenu`, `SysDept` | |
| 106 | | Pydantic Schema | 功能 + 后缀 | `UserCreate`, `UserUpdate`, `UserVO`, `UserQuery` | |
| 107 | | Service 类 | 模块名 + `Service` | `UserService`, `AuthService`, `RoleService` | |
| 108 | | Config 类 | `Settings` | `Settings`(单例) | |
| 109 | | Enum 类 | 功能名 + `Enum` | `StatusEnum`, `DataScopeEnum` | |
| 110 | | Exception | `BusinessException` | 统一使用 `BusinessException` | |
| 111 | |
| 112 | **Schema 后缀规范**: |
| 113 | |
| 114 | | 后缀 | 用途 | 示例 | |
| 115 | |------|------|------| |
| 116 | | `Create` | 创建表单 | `UserCreate`, `RoleCreate` | |
| 117 | | `Update` | 更新表单 | `UserUpdate`, `MenuUpdate` | |
| 118 | | `VO` | 视图对象(返回) | `UserVO`, `RoleVO`, `DictVO` | |
| 119 | | `Query` | 分页查询参数 | `UserQuery`, `RoleQuery`, `LogQuery` | |
| 120 | | `Form` | 特定操作表单 | `UserPasswordForm`, `RoleMenuForm` | |
| 121 | | `Result` | 响应结果 | `LoginResult`, `ExcelResultVO` | |
| 122 | |
| 123 | ### 3.3 方法命名 |
| 124 | |
| 125 | | 操作 | 方法名 | 示例 | |
| 126 | |------|--------|------| |
| 127 | | 分页查询 | `get_page` | `async def get_page(self, query: UserQuery) -> PageResult` | |
| 128 | | 单条查询 | `get_by |