# template-fastapi **Repository Path**: gaochuanaaa/template-fastapi ## Basic Information - **Project Name**: template-fastapi - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-02-29 - **Last Updated**: 2024-04-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # FastAPI Web 种子工程 - 一些生产配置 - 带有动态工作者配置的 gunicorn - 经过优化的 Dockerfile,体积小,可使用非 root 用户快速构建 - JSON 日志 - 用于部署环境的 sentry - 易于本地开发 - 已配置 postgres 和 redis 的环境 - 使用 "black "和 "ruff "脚本检查代码 - 使用 `async-asgi-testclient`, `pytest-env`, `pytest-asyncio` 配置 pytest - 略微配置了 `alembic` 的 SQLAlchemy - 异步 SQLAlchemy 引擎 - 迁移设置为易于排序的格式(`YYYY-MM-DD_slug`)。 - 预装 JWT 授权 - 短期访问令牌 - 存储在只通过 http 访问的 cookies 中的长期刷新令牌 - 使用 "bcrypt "存储加盐密码 - 具有以下功能的全局 pydantic 模型 - 在导出 JSON 时明确设置时区 - 以及其他一些额外功能,如全局异常、sqlalchemy 键命名约定、alembic 的快捷脚本等。 ## 快速开始 ### 第一步 1. `cp .env.example .env` 2. `docker network create app_main` 3. `docker-compose up -d --build` ### Linters Format the code with `ruff --fix` and black ```shell docker compose exec app format ``` ### 数据库脚本迁移Migrations - Create an automatic migration from changes in `src/database.py` ```shell docker compose exec app makemigrations *migration_name* ``` - 执行数据库迁移 ```shell docker compose exec app migrate ``` - 迁移回退版本 ```shell docker compose exec app downgrade -1 # or -2 or base or hash of the migration ``` ### 测试 所有测试都是集成测试,需要数据库连接。 使用默认数据库(`postgres`),与应用程序的`app`数据库分开。 - 使用默认数据库可以更方便地在 CI/CD 环境中运行测试,因为无需设置额外的数据库 - 通过升级和降级 alembic 迁移来运行测试。 执行测试 ```shell docker compose exec app pytest ``` ### 样例介绍 启动脚本可以参考start-dev.sh和start-prod.sh,使用uvicorn启动FastAPI应用,配置文件为.env - `src/controller` 包含了一个简单的样例路由,GET POST SSE 请求样例,展示FastAPI的一些特性 - `src/models.py` 包含了一个简单的样例模型,展示了如何使用pydantic模型 - `src/database.py` 包含了一个简单的样例数据库连接,展示了如何使用SQLAlchemy - `src/auth.py` 包含了一个简单的样例JWT授权,展示了如何使用JWT授权 - `src/exceptions.py` 包含了一个简单的样例全局异常,展示了如何使用全局异常 - `src/alembic` 包含了一个简单的样例alembic迁移,展示了如何使用alembic迁移 - `src/my_redis.py` 包含了一个简单的样例redis连接,展示了如何使用redis - `src/config.py` 包含了一些简单的配置,展示了如何使用一些简单的配置 - `src/main.py` 包含了一个简单的样例FastAPI应用,展示了如何使用FastAPI - `src/spark` 包含了一个简单样例,用于调用星火大模型 接口调用样例apipost参考 https://console-docs.apipost.cn/preview/38544165b72ebb9c/a8f310f816860662