|
| 1 | +# 全栈 FastAPI 模板 |
| 2 | + |
| 3 | +<a href="https://github.com/fastapi/full-stack-fastapi-template/actions?query=workflow%3ATest" target="_blank"><img src="https://github.com/fastapi/full-stack-fastapi-template/workflows/Test/badge.svg" alt="测试"></a> |
| 4 | +<a href="https://coverage-badge.samuelcolvin.workers.dev/redirect/fastapi/full-stack-fastapi-template" target="_blank"><img src="https://coverage-badge.samuelcolvin.workers.dev/fastapi/full-stack-fastapi-template.svg" alt="代码覆盖率"></a> |
| 5 | + |
| 6 | +## 技术栈及功能 |
| 7 | + |
| 8 | +- ⚡ 后端 API 使用 [**FastAPI**](https://fastapi.tiangolo.com)。 |
| 9 | + - 🧰 [SQLModel](https://sqlmodel.tiangolo.com) 处理 Python 的 SQL 数据库交互(ORM)。 |
| 10 | + - 🔍 [Pydantic](https://docs.pydantic.dev)(FastAPI 的依赖)负责数据验证和配置管理。 |
| 11 | + - 💾 SQL 数据库采用 [PostgreSQL](https://www.postgresql.org)。 |
| 12 | +- 🚀 前端使用 [React](https://react.dev)。 |
| 13 | + - 💃 使用 TypeScript、hooks、Vite 等现代前端技术。 |
| 14 | + - 🎨 前端组件基于 [Chakra UI](https://chakra-ui.com)。 |
| 15 | + - 🤖 自动生成的前端客户端。 |
| 16 | + - 🧪 使用 [Playwright](https://playwright.dev) 进行端到端测试。 |
| 17 | + - 🦇 支持暗黑模式。 |
| 18 | +- 🐋 使用 [Docker Compose](https://www.docker.com) 进行开发和生产环境配置。 |
| 19 | +- 🔒 默认支持安全的密码哈希。 |
| 20 | +- 🔑 基于 JWT(JSON Web Token)的认证机制。 |
| 21 | +- 📫 基于电子邮件的密码找回功能。 |
| 22 | +- ✅ 使用 [Pytest](https://pytest.org) 进行测试。 |
| 23 | +- 📞 采用 [Traefik](https://traefik.io) 作为反向代理/负载均衡器。 |
| 24 | +- 🚢 提供基于 Docker Compose 的部署指南,包括如何使用 Traefik 前端代理配置自动 HTTPS 证书。 |
| 25 | +- 🏭 基于 GitHub Actions 的 CI(持续集成)和 CD(持续部署)。 |
| 26 | + |
| 27 | +### 仪表盘登录 |
| 28 | + |
| 29 | +[](https://github.com/fastapi/full-stack-fastapi-template) |
| 30 | + |
| 31 | +### 仪表盘 - 管理员界面 |
| 32 | + |
| 33 | +[](https://github.com/fastapi/full-stack-fastapi-template) |
| 34 | + |
| 35 | +### 仪表盘 - 创建用户 |
| 36 | + |
| 37 | +[](https://github.com/fastapi/full-stack-fastapi-template) |
| 38 | + |
| 39 | +### 仪表盘 - 数据项 |
| 40 | + |
| 41 | +[](https://github.com/fastapi/full-stack-fastapi-template) |
| 42 | + |
| 43 | +### 仪表盘 - 用户设置 |
| 44 | + |
| 45 | +[](https://github.com/fastapi/full-stack-fastapi-template) |
| 46 | + |
| 47 | +### 仪表盘 - 暗黑模式 |
| 48 | + |
| 49 | +[](https://github.com/fastapi/full-stack-fastapi-template) |
| 50 | + |
| 51 | +### 交互式 API 文档 |
| 52 | + |
| 53 | +[](https://github.com/fastapi/full-stack-fastapi-template) |
| 54 | + |
| 55 | +## 使用方法 |
| 56 | + |
| 57 | +您可以 **直接 fork 或克隆** 此仓库并按需使用。 |
| 58 | + |
| 59 | +✨ 它开箱即用。✨ |
| 60 | + |
| 61 | +### 如何使用私人仓库 |
| 62 | + |
| 63 | +如果您希望使用私人仓库,由于 GitHub 不允许更改 fork 仓库的可见性,您无法直接 fork 此仓库。 |
| 64 | + |
| 65 | +但您可以按照以下步骤操作: |
| 66 | + |
| 67 | +- 创建一个新的 GitHub 仓库,例如 `my-full-stack`。 |
| 68 | +- 手动克隆此仓库,并将名称设置为您想使用的项目名称,例如 `my-full-stack`: |
| 69 | + |
| 70 | +```bash |
| 71 | +git clone [email protected]:fastapi/full-stack-fastapi-template.git my-full-stack |
| 72 | +``` |
| 73 | + |
| 74 | +- 进入新目录: |
| 75 | + |
| 76 | +```bash |
| 77 | +cd my-full-stack |
| 78 | +``` |
| 79 | + |
| 80 | +- 将远程仓库的地址设置为您新创建的仓库,可以从 GitHub 界面复制地址,例如: |
| 81 | + |
| 82 | +```bash |
| 83 | +git remote set-url origin [email protected]:octocat/my-full-stack.git |
| 84 | +``` |
| 85 | + |
| 86 | +- 将此模板仓库添加为另一个 "remote",以便以后获取更新: |
| 87 | + |
| 88 | +```bash |
| 89 | +git remote add upstream [email protected]:fastapi/full-stack-fastapi-template.git |
| 90 | +``` |
| 91 | + |
| 92 | +- 将代码推送到您新的仓库: |
| 93 | + |
| 94 | +```bash |
| 95 | +git push -u origin master |
| 96 | +``` |
| 97 | + |
| 98 | +### 从原始模板更新 |
| 99 | + |
| 100 | +在克隆仓库并进行修改后,您可能需要从此原始模板中获取最新的更改。 |
| 101 | + |
| 102 | +- 确保您已将原始仓库添加为远程仓库,可以通过以下命令检查: |
| 103 | + |
| 104 | +```bash |
| 105 | +git remote -v |
| 106 | + |
| 107 | +origin [email protected]:octocat/my-full-stack.git (fetch) |
| 108 | +origin [email protected]:octocat/my-full-stack.git (push) |
| 109 | +upstream [email protected]:fastapi/full-stack-fastapi-template.git (fetch) |
| 110 | +upstream [email protected]:fastapi/full-stack-fastapi-template.git (push) |
| 111 | +``` |
| 112 | + |
| 113 | +- 拉取最新的更改但不合并: |
| 114 | + |
| 115 | +```bash |
| 116 | +git pull --no-commit upstream master |
| 117 | +``` |
| 118 | + |
| 119 | +这会下载此模板的最新更改但不会立即提交,您可以在提交前检查所有内容是否正确。 |
| 120 | + |
| 121 | +- 如果存在冲突,请在编辑器中解决它们。 |
| 122 | + |
| 123 | +- 完成后,提交更改: |
| 124 | + |
| 125 | +```bash |
| 126 | +git merge --continue |
| 127 | +``` |
| 128 | + |
| 129 | +### 配置 |
| 130 | + |
| 131 | +您可以在 `.env` 文件中更新配置,以定制您的项目设置。 |
| 132 | + |
| 133 | +在部署前,至少需要更改以下配置项的值: |
| 134 | + |
| 135 | +- `SECRET_KEY` |
| 136 | +- `FIRST_SUPERUSER_PASSWORD` |
| 137 | +- `POSTGRES_PASSWORD` |
| 138 | + |
| 139 | +建议(并且应该)通过环境变量将这些配置从安全存储中传递。 |
| 140 | + |
| 141 | +详情请参阅 [deployment.md](./deployment.md) 文档。 |
| 142 | + |
| 143 | +### 生成密钥 |
| 144 | + |
| 145 | +`.env` 文件中的某些环境变量默认值为 `changethis`。 |
| 146 | + |
| 147 | +您需要用一个密钥替换这些默认值。可以运行以下命令生成密钥: |
| 148 | + |
| 149 | +```bash |
| 150 | +python -c "import secrets; print(secrets.token_urlsafe(32))" |
| 151 | +``` |
| 152 | + |
| 153 | +复制生成的内容,用作密码或密钥。运行命令多次可生成不同的安全密钥。 |
| 154 | + |
| 155 | +--- |
| 156 | + |
| 157 | +## 如何使用 - 替代方式:使用 Copier |
| 158 | + |
| 159 | +此仓库支持通过 [Copier](https://copier.readthedocs.io) 生成新项目。 |
| 160 | + |
| 161 | +Copier 会复制所有文件,询问您一些配置问题,并根据您的回答更新 `.env` 文件。 |
| 162 | + |
| 163 | +### 安装 Copier |
| 164 | + |
| 165 | +您可以通过以下命令安装 Copier: |
| 166 | + |
| 167 | +```bash |
| 168 | +pip install copier |
| 169 | +``` |
| 170 | + |
| 171 | +如果使用 [`pipx`](https://pipx.pypa.io/),可以通过以下方式运行: |
| 172 | + |
| 173 | +```bash |
| 174 | +pipx install copier |
| 175 | +``` |
| 176 | + |
| 177 | +**注意**:如果已安装 `pipx`,安装 Copier 是可选的,您可以直接运行它。 |
| 178 | + |
| 179 | +### 使用 Copier 生成项目 |
| 180 | + |
| 181 | +为您的新项目目录命名,例如 `my-awesome-project`。 |
| 182 | + |
| 183 | +进入目标父目录,并运行以下命令生成项目: |
| 184 | + |
| 185 | +```bash |
| 186 | +copier copy https://github.com/fastapi/full-stack-fastapi-template my-awesome-project --trust |
| 187 | +``` |
| 188 | + |
| 189 | +如果您使用了 `pipx`,且未安装 Copier,可以直接运行: |
| 190 | + |
| 191 | +```bash |
| 192 | +pipx run copier copy https://github.com/fastapi/full-stack-fastapi-template my-awesome-project --trust |
| 193 | +``` |
| 194 | + |
| 195 | +**注意**:`--trust` 选项是必须的,它允许执行一个 [创建后脚本](https://github.com/fastapi/full-stack-fastapi-template/blob/master/.copier/update_dotenv.py),以更新您的 `.env` 文件。 |
| 196 | + |
| 197 | +--- |
| 198 | + |
| 199 | +### 输入变量 |
| 200 | + |
| 201 | +Copier 会要求您提供一些配置数据。生成项目前,您可以先准备好这些数据。 |
| 202 | + |
| 203 | +但不用担心,生成后您可以随时在 `.env` 文件中修改这些设置。 |
| 204 | + |
| 205 | +以下是输入变量及其默认值(部分值会自动生成): |
| 206 | + |
| 207 | +- `project_name`:(默认值:`"FastAPI Project"`)项目名称,展示给 API 用户(在 .env 中)。 |
| 208 | +- `stack_name`:(默认值:`"fastapi-project"`)Docker Compose 的标签和项目名(无空格,无句号)(在 .env 中)。 |
| 209 | +- `secret_key`:(默认值:`"changethis"`)项目密钥,用于安全性(在 .env 中)。可使用上述方法生成。 |
| 210 | +- `first_superuser`:(默认值: `"[email protected]"`)首位超级用户的邮箱(在 .env 中)。 |
| 211 | +- `first_superuser_password`:(默认值:`"changethis"`)首位超级用户的密码(在 .env 中)。 |
| 212 | +- `smtp_host`:(默认值:`""`)发送邮件的 SMTP 服务器地址,可在 .env 中稍后设置。 |
| 213 | +- `smtp_user`:(默认值:`""`)发送邮件的 SMTP 用户,可在 .env 中稍后设置。 |
| 214 | +- `smtp_password`:(默认值:`""`)发送邮件的 SMTP 密码,可在 .env 中稍后设置。 |
| 215 | +- `emails_from_email`:(默认值: `"[email protected]"`)发送邮件的账户,可在 .env 中稍后设置。 |
| 216 | +- `postgres_password`:(默认值:`"changethis"`)PostgreSQL 数据库密码(在 .env 中)。可使用上述方法生成。 |
| 217 | +- `sentry_dsn`:(默认值:`""`)Sentry 的 DSN,如果使用,可在 .env 中稍后设置。 |
| 218 | + |
| 219 | +--- |
| 220 | + |
| 221 | +## 后端开发 |
| 222 | + |
| 223 | +后端文档:[backend/README.md](./backend/README.CN.md) |
| 224 | + |
| 225 | +## 前端开发 |
| 226 | + |
| 227 | +前端文档:[frontend/README.md](./frontend/README.CN.md) |
| 228 | + |
| 229 | +## 部署 |
| 230 | + |
| 231 | +部署文档:[deployment.md](./deployment.CN.md) |
| 232 | + |
| 233 | +## 开发 |
| 234 | + |
| 235 | +通用开发文档:[development.md](./development.CN.md) |
| 236 | + |
| 237 | +内容包括使用 Docker Compose、自定义本地域名、`.env` 配置等。 |
| 238 | + |
| 239 | +## 更新日志 |
| 240 | + |
| 241 | +请查看 [release-notes.md](./release-notes.md)。 |
| 242 | + |
| 243 | +--- |
| 244 | + |
| 245 | +## 许可证 |
| 246 | + |
| 247 | +全栈 FastAPI 模板基于 MIT 许可证授权使用。 |
0 commit comments