fastapi-file-structure

fastapi-file-structure is a cursor rule for Cursor from holtwood/awesome-cursorrules-zh. It costs 4 tokens per session (1,215 once invoked), scanned A, original, MIT.

A recommended FastAPI project structure that separates routes, configuration, security, database sessions, create/read/update/delete operations, models, and schemas into modules.

In plain words
What is it for?
Use it when organising a new FastAPI project or restructuring an existing one.
Why use it?
It keeps application parts separate, making the codebase easier to maintain and extend.

Cursor rule for Cursor

Written for Cursor: a Cursor rule (.mdc).

Good fit Use it when organising a new FastAPI project or restructuring an existing one.

Compare 6 cursor rules from other repositories ↓
Install with agentmods
npx agentmods add rules/holtwood/awesome-cursorrules-zh/fastapi-file-structure
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Clone the repo
git clone --depth 1 https://github.com/holtwood/awesome-cursorrules-zh

Made for: Cursor.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for fastapi-file-structure

README.md
[![agentmods](https://agentmods.dev/badge/rules/holtwood/awesome-cursorrules-zh/fastapi-file-structure/github.svg)](https://agentmods.dev/rules/holtwood/awesome-cursorrules-zh/fastapi-file-structure)
Your own site
<a href="https://agentmods.dev/rules/holtwood/awesome-cursorrules-zh/fastapi-file-structure"><img src="https://agentmods.dev/badge/rules/holtwood/awesome-cursorrules-zh/fastapi-file-structure/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for fastapi-file-structure

Your own site · 80×15
<a href="https://agentmods.dev/rules/holtwood/awesome-cursorrules-zh/fastapi-file-structure"><img src="https://agentmods.dev/badge/rules/holtwood/awesome-cursorrules-zh/fastapi-file-structure.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 4 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 1,215 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00004 $0.01215
Opus 5 $0.00002 $0.00607
Sonnet 5 $0.00001 $0.00243
Haiku 4.5 $0.00000 $0.00121

Measured 6d ago against content hash cebacb4c0eae, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-09, from the pricing page.

Security

Grade A, and why

fastapi-file-structure scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 6d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

docs/rules/backend/python/fastapi-api-example/fastapi-file-structure.mdc · 101 lines

How it starts

The opening of the file, as written. The whole thing — 101 lines — stays where its author put it; the contents beside it link to each section on GitHub.

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 镜像的配置文件。

Read the full file on GitHub · 101 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 6d ago First seen · 101 lines · 4 tokens per session scan A cebacb4c0eae

Subscribe to this mod's changes

fastapi-file-structure is a cursor rule published in the GitHub repository holtwood/awesome-cursorrules-zh (233 stars, last pushed 1mo ago), licensed MIT. It adds 4 tokens to every session and 1,215 once invoked, about $0.0000 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-03.