# fast-api-demo **Repository Path**: irooit/fast-api-demo ## Basic Information - **Project Name**: fast-api-demo - **Description**: 基于 FastApi 研发的一个基础 Demo Api 架构, 带验证Token信息, 易扩展,易维护, 可用于微服务, MySQL, Redis, jwt - **Primary Language**: Python - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 3 - **Created**: 2023-03-23 - **Last Updated**: 2023-03-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 基于 FastApi 研发的一个基础Demo Api架构, 带验证Token信息 #### 相关知识 ```text 框架: FastApi 缓存: Redis, 数据库: Mysql, Token: Jwt ``` #### 已开发功能 > 登录验证 > > 账户登录 -> 获取 access_token
> > 密码修改
> > 添加登录
> > 删除登录
> 用户 > > 用户列表
> > 添加用户
> > 编辑用户
> > 删除用户 > 轮播图 > > 轮播图列表
> > 添加轮播图
> > 编辑轮播图
> > 删除轮播图 #### 软件架构 ```text app -- 代码 endpoints -- 路由, 接收参数 __init__.py -- 每增加一个模块, 需要这里注册 repositories -- DB, 与数据库交互, 每增加一个模块, 需要注册 __init__.py __init__.py -- 每增加一个模块, 需要这里注册 services -- 业务处理, 每增加一个模块, 需要注册 __init__.py __init__.py -- 每增加一个模块, 需要这里注册 __init__.py -- 注册 conf.py -- 读取config.yml containers.py -- 依赖注入 db.py -- 数据库配置 exceptions.py -- 异常 logger.py -- 日志 models.py -- models redis.py -- redis resp.py -- 返回初始化 schemas.py -- 参数验证, 如果参数过多, 建议拆分, 继承公告参数 security.py -- 加密解密, token utils.py -- 公共方法 config -- 配置 api.ini --supversior 配置 config.dev.yml --测试开发配置文件 config.prod.yml --生产环境配置文件 logs --日志 .gitignore --git 忽略文件 boot.sh --生产环境部署脚本 cmd.py --启动文件 config.py --生产环境配置 config.yml --配置文件 docker-compose.yml --docker compose Dockerfile --dockerfile docker.sh --容器内执行脚本 README.md --文档 requirements.txt --依赖包 ``` #### 版本, 当前版本为3.11.2 ```shell python 3.11.2 mysql 8.x ``` #### 依赖环境安装 ```shell pip install -r requirements.txt ``` #### 配置复制 ```shell cp config/config.dev.yml config.yml ``` #### 修改配置 ```text 新建数据库, 配置信息填充 ``` #### 数据库初始化 ```shell 0. 开发环境下, config.yml 的 app.debug: True的话, 启动时候数据库没表的自动建表 1. 初始化数据库 # 生成初始化数据配置, 生成后会生成一个aerich.ini文件和一个migrations文件夹 aerich init -t app.db.TORTOISE_ORM # 初始化数据库 aerich init-db 2. 已经初始化过的, 修改迁移 # 修改数据模型后生成迁移文件, 在后面加 --name=xxx, 可以指定文件名 aerich migrate # 执行迁移 aerich upgrade # 回到上一个版本 aerich downgrade # 查看历史迁移记录 aerich history # 查看形成当前版本的迁移记录文件 aerich heads ``` #### 开发环境 ```shell python cmd.py or uvicorn cmd:app --reload --host 0.0.0.0 --port 8888 ``` #### 生产环境 ```text cd /path/demo echo "env=\'dev\'" > ~/.bashrc chmod +x boot.sh . boot.sh ``` #### model 相关文档: ```text https://tortoise-orm.readthedocs.io/en/latest/migration.html#init-db https://tortoise-orm.readthedocs.io/en/latest/reference.html ``` [tortoise-orm](https://tortoise-orm.readthedocs.io/en/latest/reference.html) ### docker 部署 ```shell # dockerfile 基本固定, redis密码默认12345678 # 主要是 docker-compose.yml, 注意共享目录和端口就行 # 这里存放的目录是:/opt/app/fast mkdir -p /opt/app/fast # git clone 代码 git clone https://gitee.com/app-server-list/fast-api-demo.git cd fast-api-demo # 开始部署 docker-compose up -d # 删除整个容器 docker-compose down # 更新脚本 chmod +x docker.sh docker exec -it FastApi /opt/app/code/docker.sh # 查看状态 docker exec -it FastApi supervisorctl status ```