
快速开始
约 1038 字
警告
fba 面向具备一定 Python 后端经验的开发者。如果你刚开始学习 FastAPI,建议先熟悉 Python 异步编程、FastAPI、SQLAlchemy 和 Pydantic 的基础用法
注意
自 fba 2.0.0 起,fba 已移除 MySQL 支持,仅支持 PostgreSQL 数据库
本地开发
准备本地环境
准备源码 二选一
拉取源代码git clone https://github.com/fastapi-practices/fastapi-best-architecture.git创建模板仓库此项目支持创建模板仓库,你可以直接创建一个非 fork 的个人仓库。如图所示,进入此项目 GitHub 首页,使用
use this template按钮创建创建完成之后,使用
git clone命令拉取你自己的仓库即可
启动 PostgreSQL、Redis
初始化
自动在
根目录打开终端,执行以下命令uv run fba init --auto手动创建数据库:
fba- PostgreSQL 用户直接创建
env
在
backend目录打开终端,执行以下命令创建环境变量文件cp .env.example .env按需修改配置文件:
backend/core/conf.py和.env安装依赖
uv - syncuv syncuv - pipuv pip install -r requirements.txt创建数据库表和测试数据 二选一
CLI在
根目录打开终端(确保已激活虚拟环境),执行以下命令fba initAlembic + 手动在
根目录打开终端(确保已激活虚拟环境),执行以下命令生成迁移文件
fba alembic revision执行迁移
fba alembic upgrade初始化测试数据
架构:执行
backend/sql/postgresql/目录下对应主键模式的脚本插件:执行
backend/plugin/{plugin}/sql/postgresql/目录下对应主键模式的脚本fba --sql 脚本文件路径
启动
在
根目录打开终端(确保已激活虚拟环境),执行以下命令fba run启动 celery worker, beat 和 flower 此步骤为可选,可直接跳过
在
根目录打开终端(确保已激活虚拟环境),执行以下命令启动 celery 相关服务Workerfba celery workerBeatfba celery beatFlowerfba celery flower注意
如果从未执行过以上命令,任务结果表将缺失,此时,无论从何处调用任务结果相关接口都会直接报错,直到至少启动一次 worker 和 beat 服务,相关接口将自动恢复正常
打开浏览器访问:http://127.0.0.1:8000/docs
开发流程
提示
使用 AI 辅助开发时,建议先安装 ,再按 model、schema、router、service、crud 的顺序生成或审查代码
注
仅供参考,实际以个人开发习惯为准
单元测试
相关信息
通过 pytest 运行单元测试。项目内仅提供基础测试示例,并不是完整业务测试套件;如需覆盖实际业务,请按项目需求补充测试用例
创建测试数据库
fba_test创建数据库表,利用工具创建
fba库所有表的 DDL 脚本,再通过fba_test库执行初始化测试数据,通过
backend/sql/postgresql/目录下对应主键模式的脚本初始化测试数据在项目根目录打开终端,执行以下单元测试命令
pytest -vs --disable-warnings
更新日志
8049d-update docs于06c36-update docs于4704f-update docs于b1c46-update docs于35629-update docs于754ce-update docs于3b2fc-update docs于a15a7-update docs于d539f-update docs于b5620-update docs于f87be-update docs于d84bf-update docs于a573f-update docs于52a1c-update docs于f3a62-update docs于bec18-update docs于125c9-update docs于f09c5-update docs于7186a-update docs于d6792-update docs于b99d3-update docs于65467-update docs于b0c50-update docs于180e1-update docs于75109-update docs于599bc-update docs于fb59d-update docs于82bbd-update docs于c79b7-update docs于12719-update docs于ace06-update docs于edad3-update docs于54a83-update docs于46aa1-update docs于6a41a-update docs于039ff-update docs于64d65-update docs于8e390-update docs于13eb1-update docs于a0cbb-update docs于5aa5f-update docs于02f7e-update docs于f1515-update docs于496d8-update docs于da774-update docs于7378f-update docs于5b146-update docs于4bce7-update docs于ce9c9-update docs于0e81f-update docs于30f0d-update docs于ac49b-update docs于c641b-update docs于bd1eb-update docs于1951f-update docs于246cf-update docs于f4aee-update docs于5b2d8-update docs于f25c5-update docs于3ddd9-update docs于e7ec4-update docs于af7ae-update docs于a2e52-update docs于30121-update docs于c6ced-update docs于13e66-update docs于efeef-update docs于cc930-update docs于4f58b-initial commit (#1)于
