File contents FastAPI 文件结构
本规则集定义了在 FastAPI 应用程序中推荐的文件和目录结构,旨在提高代码的可组织性、可维护性和可扩展性。
1. 核心原则
模块化 : 将应用程序的不同部分(如路由、模型、服务、依赖项)分离到独立的模块中。
清晰性 : 文件和目录命名应清晰明了,反映其内容和功能。
可扩展性 : 结构应支持未来功能的添加和团队协作。
2. 推荐的目录结构
以下是一个推荐的 FastAPI 项目结构示例:
my_fastapi_project/
├── app/
│ ├── __init__.py
│ ├── main.py # FastAPI 应用入口点
│ ├── api/ # API 路由和端点
│ │ ├── __init__.py
│ │ ├── v1/ # API 版本控制 (可选)
│ │ │ ├── __init__.py
│ │ │ ├── endpoints/ # 具体端点实现
│ │ │ │ ├── __init__.py
│ │ │ │ ├── users.py
│ │ │ │ └── items.py
│ │ │ └── deps.py # 版本特定的依赖项
│ │ └── router.py # 聚合所有 API 路由
│ ├── core/ # 核心配置、设置、常量
│ │ ├── __init__.py
│ │ ├── config.py
│ │ └── security.py
│ ├── crud/ # 创建、读取、更新、删除操作 (与数据库交互)
│ │ ├── __init__.py
│ │ ├── user.py
│ │ └── item.py
│ ├── db/ # 数据库相关配置和会话管理
│ │ ├── __init__.py
│ │ ├── base.py # 基础模型或 ORM 声明
│ │ └── session.py # 数据库会话管理
│ ├── models/ # Pydantic 模型和 SQLAlchemy 模型
│ │ ├── __init__.py
│ │ ├── user.py
│ │ └── item.py
│ ├── schemas/ # Pydantic schema (请求/响应模型)
│ │ ├── __init__.py
│ │ ├── user.py
│ │ └── item.py
│ ├── services/ # 业务逻辑服务层
│ │ ├── __init__.py
│ │ ├── user.py
│ │ └── item.py
│ └── tests/ # 单元测试和集成测试
│ ├── __init__.py
│ ├── api/
│ │ └── test_users.py
│ └── crud/
│ └── test_user_crud.py
├── .env # 环境变量 (敏感信息)
├── .gitignore
├── Dockerfile # Docker 容器化配置
├── README.md
├── requirements.txt # Python 依赖包列表
└── uvicorn_run.sh # 启动脚本 (可选)
3. 目录说明
app/main.py: FastAPI 应用的入口文件,通常在这里实例化 FastAPI() 并包含顶层路由。
app/api/: 包含所有 API 路由定义。可以进一步按版本 (v1/, v2/) 或功能模块 (users/, items/) 组织。
app/core/: 存放应用程序的核心配置、安全设置、日志配置等。
app/crud/: 包含与数据库进行 CRUD 操作的函数。这些函数通常接收 Pydantic 模型作为输入,并返回数据库模型。
app/db/: 数据库连接、会话管理和 ORM 基础声明。
app/models/: 定义数据库模型(如 SQLAlchemy ORM 模型)。
app/schemas/: 定义 Pydantic 模型,用于请求体验证、响应序列化和数据验证。
app/services/: 包含业务逻辑。crud 层处理数据库交互,而 services 层则协调 crud 操作,实现更复杂的业务规则。
app/tests/: 存放所有测试文件,结构应与 app/ 目录相对应。
requirements.txt: 列出项目所需的所有 Python 依赖。
Dockerfile: 用于构建 Docker 镜像的配置文件。
4. 启动应用
通常使用 Uvicorn 启动 FastAPI 应用:
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
或者如果您有 uvicorn_run.sh 脚本:
bash uvicorn_run.sh
1 --- 2 name: fastapi-api-example-fastapi-file-structure 3 description: FastAPI 文件结构 4 --- 5 6 # FastAPI 文件结构 7 8 本规则集定义了在 FastAPI 应用程序中推荐的文件和目录结构,旨在提高代码的可组织性、可维护性和可扩展性。 9 10 ## 1. 核心原则 11 12 - **模块化**: 将应用程序的不同部分(如路由、模型、服务、依赖项)分离到独立的模块中。 13 - **清晰性**: 文件和目录命名应清晰明了,反映其内容和功能。 14 - **可扩展性**: 结构应支持未来功能的添加和团队协作。 15 16 ## 2. 推荐的目录结构 17 18 以下是一个推荐的 FastAPI 项目结构示例: 19 20 ``` 21 my_fastapi_project/ 22 ├── app/ 23 │ ├── __init__.py 24 │ ├── main.py # FastAPI 应用入口点 25 │ ├── api/ # API 路由和端点 26 │ │ ├── __init__.py 27 │ │ ├── v1/ # API 版本控制 (可选) 28 │ │ │ ├── __init__.py 29 │ │ │ ├── endpoints/ # 具体端点实现 30 │ │ │ │ ├── __init__.py 31 │ │ │ │ ├── users.py 32 │ │ │ │ └── items.py 33 │ │ │ └── deps.py # 版本特定的依赖项 34 │ │ └── router.py # 聚合所有 API 路由 35 │ ├── core/ # 核心配置、设置、常量 36 │ │ ├── __init__.py 37 │ │ ├── config.py 38 │ │ └── security.py 39 │ ├── crud/ # 创建、读取、更新、删除操作 (与数据库交互) 40 │ │ ├── __init__.py 41 │ │ ├── user.py 42 │ │ └── item.py 43 │ ├── db/ # 数据库相关配置和会话管理 44 │ │ ├── __init__.py 45 │ │ ├── base.py # 基础模型或 ORM 声明 46 │ │ └── session.py # 数据库会话管理 47 │ ├── models/ # Pydantic 模型和 SQLAlchemy 模型 48 │ │ ├── __init__.py 49 │ │ ├── user.py 50 │ │ └── item.py 51 │ ├── schemas/ # Pydantic schema (请求/响应模型) 52 │ │ ├── __init__.py 53 │ │ ├── user.py 54 │ │ └── item.py 55 │ ├── services/ # 业务逻辑服务层 56 │ │ ├── __init__.py 57 │ │ ├── user.py 58 │ │ └── item.py 59 │ └── tests/ # 单元测试和集成测试 60 │ ├── __init__.py 61 │ ├── api/ 62 │ │ └── test_users.py 63 │ └── crud/ 64 │ └── test_user_crud.py 65 ├── .env # 环境变量 (敏感信息) 66 ├── .gitignore 67 ├── Dockerfile # Docker 容器化配置 68 ├── README.md 69 ├── requirements.txt # Python 依赖包列表 70 └── uvicorn_run.sh # 启动脚本 (可选) 71 ``` 72 73 ## 3. 目录说明 74 75 - `app/main.py`: FastAPI 应用的入口文件,通常在这里实例化 `FastAPI()` 并包含顶层路由。 76 - `app/api/`: 包含所有 API 路由定义。可以进一步按版本 (`v1/`, `v2/`) 或功能模块 (`users/`, `items/`) 组织。 77 - `app/core/`: 存放应用程序的核心配置、安全设置、日志配置等。 78 - `app/crud/`: 包含与数据库进行 CRUD 操作的函数。这些函数通常接收 Pydantic 模型作为输入,并返回数据库模型。 79 - `app/db/`: 数据库连接、会话管理和 ORM 基础声明。 80 - `app/models/`: 定义数据库模型(如 SQLAlchemy ORM 模型)。 81 - `app/schemas/`: 定义 Pydantic 模型,用于请求体验证、响应序列化和数据验证。 82 - `app/services/`: 包含业务逻辑。`crud` 层处理数据库交互,而 `services` 层则协调 `crud` 操作,实现更复杂的业务规则。 83 - `app/tests/`: 存放所有测试文件,结构应与 `app/` 目录相对应。 84 - `requirements.txt`: 列出项目所需的所有 Python 依赖。 85 - `Dockerfile`: 用于构建 Docker 镜像的配置文件。 86 87 ## 4. 启动应用 88 89 通常使用 Uvicorn 启动 FastAPI 应用: 90 91 ```bash 92 uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload 93 ``` 94 95 或者如果您有 `uvicorn_run.sh` 脚本: 96 97 ```bash 98 bash uvicorn_run.sh 99 ```
liaosw97/awesome-rules-skills/tree/main/plugins/fastapi/skills/fastapi-api-example-fastapi-file-structure commit 0a6b25021b
Frequently asked questions How do I install the Fastapi API Example Fastapi File Structure skill? Run npx skillmds@latest add liaosw97/fastapi-api-example-fastapi-file-structure in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
What does the Fastapi API Example Fastapi File Structure skill do? FastAPI 文件结构 It is listed under Integrations & APIs on SkillMD.
Is Fastapi API Example Fastapi File Structure safe to use? This skill has not completed SkillMD's automated safety review yet. Capability flags: docs only. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
Which AI agents work with Fastapi API Example Fastapi File Structure? This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Is Fastapi API Example Fastapi File Structure free to use? Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Who published Fastapi API Example Fastapi File Structure? liaosw97 (@liaosw97) published this skill. Their other Agent Skills are listed on their SkillMD profile.