选择级别或示例
fastkit init 可以按五个级别之一创建项目,从只有一个 main.py 的项目到生产级结构。fastkit init --template 则改为复制五个示例项目之一。本页帮助你从中选出一个。
按需求选择
| 你想要 | 运行 |
|---|---|
| 从一个文件开始学习 FastAPI | fastkit init(级别 hello,即默认级别) |
| 编写一个带设置的小型服务或原型 | fastkit init --level single-file |
| 按层拆分代码:路由、CRUD、schema | fastkit init --level layered |
| 按业务领域逐步扩展 API | fastkit init --level domain |
| 部署包含数据库、用户账号、CI 和 Docker 的项目 | fastkit init --level production |
| 让所有响应(包括错误响应)都使用统一的响应封装格式 | fastkit init --template fastapi-custom-response |
| 从真正可用的用户账号开始:访问令牌和刷新令牌 | fastkit init --template fastapi-auth-jwt |
| 基于 SQLModel 和 Alembic 构建 | fastkit init --template fastapi-sqlmodel |
| 把你的 API 作为 MCP 工具提供给 AI 客户端 | fastkit init --template fastapi-mcp |
| 以流式方式返回 Claude 的聊天回复,并运行工具调用循环 | fastkit init --template fastapi-llm-agent |
各级别的详细信息:
| 级别 | 说明 | 模板 | 询问数据库 | 首先阅读 |
|---|---|---|---|---|
hello |
一个 main.py,包含 3 个端点和内存中的数据,另附测试。 | fastapi-hello |
否 | 从上到下阅读 main.py。每条注释都附有讲解对应代码的 FastAPI 教程页面链接。 |
single-file |
整个应用及其设置放在一个模块中,另附测试。 | fastapi-single-module(预设 single-module) |
是(默认:None) | 阅读 src/main.py:设置、应用和路由都在这一个模块里。 |
layered |
按层拆分:路由、CRUD、schema 和核心配置。 | fastapi-default(预设 classic-layered) |
是(默认:None) | 先阅读 src/main.py,再阅读 src/api/routes/items.py、src/crud/items.py 和 src/schemas/items.py。 |
domain |
每个领域一个包,包含路由器、服务和仓储。 | fastapi-domain-starter(预设 domain-starter) |
是(默认:None) | 先阅读 src/app/main.py,再阅读 src/app/domains/items/ 中的 router.py、service.py 和 repository.py。 |
production |
在 domain 级别的基础上增加 PostgreSQL、Alembic、JWT、CI 和 Docker。 | fastapi-domain-starter(预设 domain-starter) |
是(默认:PostgreSQL) | 先阅读 src/app/main.py,再阅读 src/app/domains/items/。README.md 会逐项介绍所选的功能。 |
从示例项目开始介绍了这五个示例项目。
级别还是 --template
级别是一个起始模板,再加上你在它之上所做的选择:hello 之后的每个级别都可以选择数据库,还可以选择自定义步骤中的功能(见自定义功能)。fastkit 会把每项选择对应的代码写入项目。
--template 则按原样复制模板,不接受数据库选项和功能选项:
$ fastkit init --template fastapi-default --level layered --project-name orders
╭────────────────── Error ───────────────────╮
│ ❌ Use --template or --level, not both. │
╰────────────────────────────────────────────╯
$ fastkit init --template fastapi-default --feature authentication=JWT --project-name orders
╭───────────────────────────────── Error ──────────────────────────────────╮
│ ❌ --template copies the template as it is; --feature applies to levels. │
╰──────────────────────────────────────────────────────────────────────────╯
fastkit list-templates 会列出所有模板,从最简单的到最复杂的依次排列。排在前面的四个模板就是各级别所基于的模板。
每个级别增加了什么
以下列表来自用 fastkit init --level <level> --no-venv -y 创建、且未使用其他任何选项的项目。
hello:main.py、tests/test_main.py、pyproject.toml、requirements.txt、requirements-dev.txt、README.md和.gitignore。没有设置模块,也没有密钥。single-file:应用移到src/main.py,设置也放在这个文件中。这个级别增加了.env(包含唯一的SECRET_KEY)和.env.example、.gitattributes,以及tests/conftest.py和tests/test_config.py。从这个级别开始,pyproject.toml中包含 pytest 和覆盖率的配置,开发依赖中包含 pytest-cov 和 ruff。layered:src/下每层一个包:api/(api.py、deps.py、routes/items.py)、core/(config.py、pagination.py、responses.py)、crud/items.py和schemas/items.py。条目保存在src/mocks/mock_items.json中。这个级别还增加了scripts/(format.sh、lint.sh、run-server.sh、test.sh),以及针对条目 API 及其存储的测试。domain:代码移到src/app/。每个业务领域都是src/app/domains/下的一个包;示例items/包含router.py、service.py、repository.py、schemas.py和models.py。src/app/api/health.py提供/health,src/app/db/memory.py把条目保存在内存中。production:在domain结构的基础上,增加 PostgreSQL(src/app/core/database.py)、Alembic(alembic.ini、alembic/、scripts/migrate.sh)、JWT 用户账号(src/app/domains/auth/)、就绪探针(src/app/features/readiness.py)、结构化日志(src/app/logging_config.py)、Dockerfile、docker-compose.yml、.dockerignore和.github/workflows/test.yml。
生成的项目介绍了 hello 之后各级别项目的共同之处。
以后再升级到更高级别
fastkit 不会把项目转换成另一个级别。需要更多结构时,请用下一个级别创建一个新项目,再把你的代码移过去。每个级别的“Next steps”面板都会给出这条命令,例如在 hello 项目中:
...
│ 7. When you want more structure, create a project at the next level. │
│ fastkit init --level single-file │
...
迁移到领域结构和数据库完整演示了一次这样的迁移。