下面给你一个 可直接复用、真正工程级、可扩展的大型 FastAPI 项目目录结构模板 。
包含:
✔ 完整目录
✔ 每个目录用途说明
✔ 推荐的初始化文件
✔ 适合大中型 Web 平台(前后端分离)
✔ 可轻松扩展成微服务、RBAC、JWT、任务队列、数据库层
✔ 适合任何 Web 网站项目直接复用
✅ 最终版 FastAPI 项目结构(专业级,可直接复用)
your_project/
│
├── app/
│ ├── api/ # 所有路由入口(按功能模块拆分)
│ │ ├── v1/ # API 版本 v1(可继续扩展 v2、v3)
│ │ │ ├── __init__.py
│ │ │ ├── users.py # 用户相关
│ │ │ ├── auth.py # 登录、JWT
│ │ │ ├── items.py # 示例接口
│ │ │ ├── tasks.py # 异步任务
│ │ │ └── ...
│ │ └── __init__.py
│ │
│ ├── core/ # 核心配置(全项目最重要)
│ │ ├── config.py # settings,数据库、JWT、环境变量等
│ │ ├── security.py # JWT、password hashing
│ │ ├── logging_config.py # 统一日志管理
│ │ └── events.py # 启动/关闭事件
│ │
│ ├── models/ # SQLAlchemy ORM 模型
│ │ ├── user.py
│ │ ├── item.py
│ │ └── ...
│ │
│ ├── schemas/ # Pydantic 模型(请求、响应)
│ │ ├── user.py
│ │ ├── item.py
│ │ └── ...
│ │
│ ├── services/ # 业务逻辑层(推荐强拆)
│ │ ├── user_service.py
│ │ ├── auth_service.py
│ │ ├── item_service.py
│ │ └── ...
│ │
│ ├── crud/ # 数据库访问层(CRUD)
│ │ ├── user.py
│ │ ├── item.py
│ │ └── ...
│ │
│ ├── db/
│ │ ├── session.py # SessionLocal 引擎
│ │ ├── base.py # Base = declarative_base()
│ │ ├── init_db.py # 数据库初始化
│ │ └── migrations/ # Alembic 迁移
│ │
│ ├── utils/ # 工具函数
│ │ ├── common.py
│ │ ├── file_ops.py
│ │ └── email.py
│ │
│ ├── middlewares/
│ │ ├── auth_middleware.py
│ │ ├── logging_middleware.py
│ │ └── cors.py
│ │
│ ├── workers/ # Celery / RQ 异步任务
│ │ └── tasks.py
│ │
│ ├── static/ # 静态文件
│ ├── templates/ # 如果启用 Jinja2 模板可放这里
│ │
│ ├── main.py # FastAPI 主入口
│ └── __init__.py
│
├── tests/ # pytest 单元测试
│ ├── test_user.py
│ └── test_auth.py
│
├── scripts/ # 脚本(初始化、批处理等)
│ └── create_user.py
│
├── alembic.ini # Alembic 配置
├── requirements.txt # pip 依赖
├── pyproject.toml # 可选(更现代)
├── README.md
└── .env # 环境变量(不会进 Git)
🔥 目录用途说明(重点)
⭐ app/api/
路由层,只做一件事情:
👉 接收 HTTP 请求,调用 Service,返回响应
遵循 单一职责原则 (SRP)。
每个文件对应模块,例如:
非常干净。
⭐ app/models/
ORM 层(SQLAlchemy)。
只负责定义数据库表结构。
⭐ app/schemas/
Pydantic 模型:
- 请求参数 Request
- 返回数据 Response
- 数据校验
⭐ app/crud/
一个模块一个 CRUD 文件。
例如 user.py:
get_user()
get_user_by_email()
create_user()
update_user()
让数据库层非常清晰。
⭐ app/services/
业务逻辑层(最关键)
把"流程+逻辑"写在这里,例如:
用户注册 = 校验数据 -> 创建用户 -> 发邮件 -> 记录日志
API 只调用 service:
python
user = UserService.register(data)
⭐ app/core/
统一配置:
- settings(环境变量)
- JWT 配置
- 加密
- Logging
- 全局事件
⭐ app/db/
数据库:
- session.py = 连接管理
- base.py = Base
- init_db.py = 初始化数据库
- migrations/ = Alembic
⭐ main.py
FastAPI 的入口:
python
app = FastAPI()
app.include_router(...)
🔥 给你一个可直接开干的 main.py 示例
python
from fastapi import FastAPI
from app.api.v1 import users, auth, items
from app.core.events import startup_event, shutdown_event
from app.middlewares.cors import setup_cors
app = FastAPI(title="My FastAPI Project")
setup_cors(app)
app.include_router(users.router, prefix="/api/v1/users")
app.include_router(auth.router, prefix="/api/v1/auth")
app.include_router(items.router, prefix="/api/v1/items")
app.add_event_handler("startup", startup_event)
app.add_event_handler("shutdown", shutdown_event)
这里有几个已经开源、标准、活跃维护的 FastAPI 模板/Boilerplate 仓库,你可以直接点击链接克隆/下载,然后作为你每次新项目的基础结构:
✅ 推荐的 FastAPI 模板仓库/链接
| 模板名称 / 仓库 | 特点 |
|---|---|
| full‑stack‑fastapi‑template | 官方推荐的 "后端 + 前端 + 部署配置" 全栈模板,包含 FastAPI + SQLModel + PostgreSQL + React + Docker 等。适合全栈项目。 (GitHub) |
| FastAPI‑boilerplate (by Benav Labs) | 偏后端/API 服务,使用 FastAPI + SQLAlchemy 2.0 + Redis + 异步 + 可扩展模块。适合构建后台服务 / 微服务。 (GitHub) |
| FastAPI‑Template (JiayuXu0) | 企业级后端模板 --- "开箱即用 + 清晰结构 + 可扩展"。适合中大型项目/团队开发。 (GitHub) |
| s3rius/FastAPI‑template | 通用、灵活的 FastAPI 模板,可根据需要选择 ORM / 数据库 /配置。适合喜欢轻量、灵活结构的项目。 (GitHub) |
🔗 仓库链接 (可以直接浏览 / 克隆)
- full-stack-fastapi-template: https://github.com/fastapi/full-stack-fastapi-template (GitHub)
- FastAPI-boilerplate (Benav Labs): https://github.com/benavlabs/FastAPI-boilerplate (GitHub)
- FastAPI-Template (JiayuXu0): https://github.com/JiayuXu0/FastAPI-Template (GitHub)
- s3rius/FastAPI-template: https://github.com/s3rius/FastAPI-template (GitHub)
✔️ 我建议你这样选
- 如果你做 全栈 Web + 前端 + 后端 +部署 → 用 full-stack-fastapi-template。
- 如果你做 仅后端 API / 微服务 / 高性能服务 → 用 FastAPI-boilerplate 或 FastAPI-Template。
- 如果你喜欢 轻量 / 可选组件 → 用 s3rius/FastAPI-template。