跳转至

自定义功能

在 hello 之后的每个级别,fastkit 都可以为项目添加功能:认证、后台任务、缓存、监控、日志、测试、迁移、实用工具、开发工具和部署文件。每个功能都附带对应的代码、设置和测试。你可以在引导式问答中选择功能,也可以用 --feature 选项选择。

自定义步骤

选好级别和数据库之后,fastkit init 会询问 Customise features?。回答 y 后,它会针对每个功能类别各问一个问题,然后询问包管理器和项目详情。直接按 Enter 键即可保留方括号中显示的默认值。

下面的会话选择了 layered 级别、SQLite、JWT 认证、Alembic 迁移和 ruff。--dry-run 只显示结果,不写入任何文件。

运行 fastkit init --no-venv --dry-run 的终端:在“Authentication Options”表格中选择了选项 1(JWT),“Project summary”中显示了“Features”一行

$ fastkit init --no-venv --dry-run

⚡ Create a FastAPI project

Project name: shop
...
Select a level [1]: 3
...
Select database [6]: 5

Customise features? [y/N]: y

🔐 Authentication Selection
Choose your authentication strategy

                             Authentication Options
┏━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Option ┃ Name          ┃ Description                                         ┃
┡━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ 1      │ JWT           │ OAuth2 password flow with JWT (recommended for      │
│        │               │ APIs)                                               │
│ 2      │ OAuth2        │ OAuth2 with authlib                                 │
│ 3      │ FastAPI-Users │ Complete user management (JWT; SQL databases only)  │
│ 4      │ Session-based │ Traditional session-based authentication            │
│ 5      │ None          │ No authentication                                   │
└────────┴───────────────┴─────────────────────────────────────────────────────┘

Select authentication method [5]: 1
...
Select migration tool [2]: 1
...
Your choice (or press Enter to skip): 1
...
Create the project in a new folder 'shop'? [Y/n]:

                                Project summary
┏━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Setting         ┃ Value                                                      ┃
┡━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ Project name    │ shop                                                       │
...
│ Database        │ SQLite                                                     │
│ Features        │ Authentication: JWT; Testing: Coverage; Migrations:        │
│                 │ Alembic; Developer tooling: ruff                           │
...

Coverage 是测试问题的默认答案,因此除非你选择其他测试选项,否则每次选择功能时都会包含它。在 hello 级别,这个问题会变成 Customise package manager and project details?,因为 hello 不接受任何功能。

用选项完成同样的选择

每个回答都有对应的选项。--feature AXIS=VALUE 选择一个功能,可以重复使用;接受多个值的功能类别用逗号分隔这些值(--feature tooling=ruff,pre-commit)。当选项已经回答了所有问题时,init 只会请你确认。下面的命令与上面的会话做出了相同的选择。加上 --dry-run 后,它还会列出将要添加的文件和包(这里是认证包),但不写入任何文件:

$ fastkit init --level layered --name shop --database SQLite --feature authentication=JWT --feature migrations=Alembic --feature tooling=ruff --dry-run
...
│ Database        │ SQLite                                                     │
│ Features        │ Authentication: JWT; Testing: Coverage; Migrations:        │
│                 │ Alembic; Developer tooling: ruff                           │
...
│   ├── auth/
...
Packages that would be installed (via uv):
...
├── pyjwt[crypto]
...
├── sqlalchemy[asyncio]
└── uvicorn[standard]>=0.52.4
╭───────────────────── Info ──────────────────────╮
│ ℹ Dry run complete — no files were written.     │
╰─────────────────────────────────────────────────╯

级别和功能列出了各功能类别的名称(即 AXIS)及其取值。--extra-package 用于添加你自己指定的包,可以重复使用。--package-manager 用于选择 pip、uv、pdm 或 poetry;不指定时,如果已安装 uv,fastkit 就使用 uv,否则使用 pip。-y 会对选项没有回答的每个问题都保留默认值:

$ fastkit init --level layered --name shop --database None --feature deployment=docker-compose --extra-package httpx --package-manager pdm --dry-run -y
...
│ Features        │ Testing: Coverage; Deployment: Docker, docker-compose      │
│ Extra packages  │ httpx                                                      │
...
│ Package manager │ pdm                                                        │
...

功能之间的规则

有些功能依赖于其他功能:

  • 选择“认证 FastAPI-Users”时,必须同时选择“数据库 PostgreSQL、MySQL 或 SQLite”。
  • 选择“部署 docker-compose”时,也会自动选择“部署 Docker”。
  • 选择“数据库 PostgreSQL 或 MySQL”时,会添加 aiosqlite(开发依赖)。
  • 选择“缓存 Redis”和“后台任务 Celery 或 Dramatiq”时,缓存和任务队列都将使用 Redis(共享依赖)。

fastkit 会在写入任何文件之前检查这些规则。违反规则的组合会被拒绝,并以退出状态码 1 退出:

$ fastkit init --level layered --name shop --database MongoDB --feature authentication=FastAPI-Users --dry-run
╭───────────────────────────────── Error ──────────────────────────────────╮
│ ❌ Authentication 'FastAPI-Users' needs a SQL database (PostgreSQL,      │
│ MySQL, SQLite), but database 'MongoDB' is selected. Choose one of those  │
│ databases, or the 'JWT' option, which works with every database.         │
╰──────────────────────────────────────────────────────────────────────────╯

列出功能

fastkit list-features 会为每个功能类别打印一张表格,列出每个选项会安装的包(包括 extras,例如 pyjwt[crypto]),最后列出每个项目都已内置的功能:

运行 fastkit list-features 的终端:“Feature Catalog”面板,以及数据库和认证的表格及其中列出的包

$ fastkit list-features
...
         Container deployment files
┏━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Option         ┃ Packages                 ┃
┡━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ Docker         │ (No additional packages) │
│ docker-compose │ (No additional packages) │
└────────────────┴──────────────────────────┘

Built into every project: CORS, Pagination.

每个选中的功能都会在生成的 README.md 中占有一节,说明它的文件和设置,并附上 FastAPI 文档的链接。