Easy FastAPI
基于 FastAPI 开发的后端框架,集成了 Tortoise ORM、Pydantic、Aerich、PyJWT、PyYAML、Redis 等插件,并且可以在编写好 models
文件后执行 manager.py gen
命令,批量生成 schemas
、routers
、services
代码,旨在提供一个高效、易用的后端开发环境。该框架通过清晰的目录结构和模块化设计,大大减少了项目的前期开发工作,帮助开发者快速构建和部署后端服务。

一、主要特点
- 集成多种插件:集成了 Tortoise(数据库 ORM)、Pydantic(数据验证和序列化)、Aerich(数据库迁移)、PyJWT(JWT 认证)、PyYAML(项目配置读取)、Redis(登出黑名单) 等常用插件,提升开发效率。
- 清晰的目录结构:通过明确的目录划分,如核心配置、数据库模型、路由、数据结构、事务处理和工具函数等,使项目结构清晰,便于维护和扩展。
- 认证授权:内置认证授权模块,支持 JWT 认证,保障系统安全。
- 数据库迁移支持:利用 Alembic 进行数据库迁移,支持自动生成迁移文件和更新数据库,确保数据库结构与代码同步。
- 代码生成器:使用内置的代码生成器,在编写好
models
的前提下,意一键生成基本 CRUD 接口,大大减轻前期开发工作
二、目录结构说明
project-root/
│
├─ backend/ # 后端项目目录(python 3.10.15)
│ ├─ app/ # fastapi 项目目录
│ │ │
│ │ ├─ handlers/ # 处理器目录
│ │ │ ├─ authentication.py # 认证处理器
│ │ │ ├─ exception.py # 异常处理器
│ │ │ └─ ...
│ │ │
│ │ ├─ models/ # 数据库模型目录
│ │ │ ├─ *.py # 数据库模型(user、role 等)
│ │ │ └─ ...
│ │ │
│ │ ├─ routers/ # 路由目录(定义路由相关信息)
│ │ │ ├─ *_router.py # 路由(user_router、role_router 等)
│ │ │ └─ ...
│ │ │
│ │ ├─ schemas/ # pydantic 数据结构目录(定义请求响应参数结构)
│ │ │ ├─ *.py # 参数模型
│ │ │ └─ ...
│ │ │
│ │ ├─ services/ # 事务处理目录(实现路由对应的逻辑)
│ │ │ ├─ *_service.py # 事务逻辑处理函数(user_service 等)
│ │ │ └─ ...
│ │ │
│ │ ├─ utils/ # 工具函数目录
│ │ │ └─ ...
│ │ │
│ │ ├─ __init__.py # 导入路、初始化配置、导入错误处理
│ │ └─ main.py # 程序入口
│ │
│ ├─ logs/ # 日志目录
│ │ ├─ access.log # 访问日志
│ │ └─ default.log # 默认日志
│ │
│ ├─ test/ # 测试目录
│ │ └─ test_*.py # 测试文件
│ │
│ ├─ easy_fastapi.yaml # 项目配置
│ └─ log_config.json # uvicorn 日志配置
│
├─ frontend/ # 前端项目目录
│ └─ ...
│
└─ ...
三、规约
正例:
if not await verify_password(form_data.password, user.h_pwd):
raise FailureException('密码错误')
反例:
if not await verify_password(form_data.password, user.h_pwd):
return JSONResult.failure('密码错误')
四、部署
- 安装依赖
pip install easy_fastapi
- 初始化项目
easy_fastapi init
- 启动项目
- 切换工作目录
cd <项目名称>/backend
- 启动项目
- 开发环境:
easy_fastapi run --reload
等价于 uvicorn app:app --reload
- 生产环境:
easy_fastapi run
等价于 uvicorn app:app --log-config log_config.json --log-level warning
五、开发
注:所有脚手架命令均在 <项目名称>/backend
目录下执行。
- 修改
backend/app/easy_fastapi.yaml
中相关配置
- 添加或修改
backend.app.models
中的模型
- 执行代码生成器
easy_fastapi gen
生成基本业务代码
- 根据实际清空添加或修改业务代码
- 创建数据库
- 初始化数据库
- 初始化 Aerich 配置
easy_fastapi db init
- 初始化数据库
easy_fastapi db init-db
- 初始化表
easy_fastapi db init-table
六、测试
- 在
backend/test
目录中添加测试文件
- 运行
cd backend && pytest test
查看测试结果
七、附录
1. 配置模板
easy_fastapi:
force_200_code: False
upload_dir: ./upload
spa:
enabled: True
index_file: ../frontend/index.html
static_dir: ../frontend/assets
static_url: /assets
authentication:
secret_key: easy_fastapi
iss: easy_fastapi
token_url: /token
login_url: /login
refresh_url: /refresh
algorithm: HS256
access_token_expire_minutes: 30
refresh_token_expire_minutes: 10080
fastapi:
root_path: /api
swagger:
title: Easy FastAPI
description: 基于 FastAPI 开发的后端框架,集成了 Tortoise ORM、Pydantic、Aerich、PyJWT、PyYAML、Redis 等插件,旨在提供一个高效、易用的后端开发环境。该框架通过清晰的目录结构和模块化设计,帮助开发者快速构建和部署后端服务。
version: ~
contact:
name: one-ccs
url: ~
email: one-ccs@foxmail.com
license:
name: 开源协议:MIT
url: https://github.com/one-ccs/easy_fastapi?tab=MIT-1-ov-file#readme
token_url: /token
docs_url: /api-docs
redoc_url: /api-redoc
openapi_url: /api-json
middleware:
cors:
enabled: False
allow_origins:
- '*'
allow_credentials: True
allow_methods:
- '*'
allow_headers:
- '*'
https_redirect:
enabled: False
trusted_host:
enabled: False
allowed_hosts:
- '*'
gzip:
enabled: False
minimum_size: 1000
compresslevel: 5
database:
username: root
password: '123456'
database: easy_fastapi
host: 127.0.0.1
port: 3306
echo: False
timezone: Asia/Chongqing
redis:
enabled: False
host: 127.0.0.1
password: ~
port: 6379
db: 0
decode_responses: True