一个现代的、可用于生产CRUD的 FastAPI模板,用于构建可扩展的 API。
主要亮点:
- -⚡️ 使用 FastAPI 和 SQLAlchemy 进行异步优先
- - ️ 干净、可维护的架构(存储库模式)
- -️ 最新的 Python 工具(UV 包管理器)
- - 自动化测试和 CI 管道
- - 一键部署至 Railway
该模板实现了一个有趣的超级英雄 API,以展示现实世界的模式!♂️
技术优势:
- -✅ 始终提供类型提示
- - Pydantic v2 用于验证
- - 自动 OpenAPI 文档
- - ⚠️ 正确的错误处理
- - 异步数据库操作
- -⚡️ 自动迁移
项目结构:
api/ ├── core/ Core functionality核心功能 │ ├── config.py Environment and app configuration环境和应用程序配置 │ ├── database.py Database connection and sessions │ ├── exceptions.py Global exception handlers │ ├── logging.py Logging configuration │ └── security.py Authentication and security ├── src/ │ ├── heroes/ Heroes module │ │ ├── models.py Database models │ │ ├── repository.py Data access layer │ │ ├── routes.py API endpoints │ │ └── schemas.py Pydantic models │ └── users/ Users module │ ├── models.py User models │ ├── repository.py User data access │ ├── routes.py User endpoints │ └── schemas.py User schemas ├── utils/ Utility functions └── main.py Application entry point应用入口
|
API 端点
- GET /heroes- 列出所有英雄
- GET /heroes/{id}- 获得特定英雄
- POST /heroes- 创建新英雄
- PATCH /heroes/{id}- 更新英雄
- DELETE /heroes/{id}- 删除英雄
验证:
- POST /auth/register- 注册新用户
- POST /auth/login- 登录并获取访问令牌
- GET /auth/me- 获取当前用户资料