工具项目地址:https://github.com/ibbd-dev/fastapi-start
# linux
sudo -H pip3 install -r https://github.com/ibbd-dev/fastapi-start/raw/main/requirements.txt
sudo -H pip3 install git+https://github.com/ibbd-dev/fastapi-start.git
# windows
pip install -r https://github.com/ibbd-dev/fastapi-start/raw/main/requirements.txt
pip install git+https://github.com/ibbd-dev/fastapi-start.git
OR
# 源码安装pip install -r requiresment.txteasy_install .
安装成功之后,会有两个命令
fastapi-start
: 完整命令fas
: 简单命令(完整命令的别名),实现功能和完整命令一样日常使用简单命令即可。
# 项目初始化
# test是项目名称,可以指定为自己的项目名称
# --title and --desc: 项目的标题及描述
fas project-init test --title=测试项目 --desc=这是一个测试项目
# 项目根目录
# 项目代码目录
cd test/app
# 启动http服务
uvicorn main:app --reload --host 0.0.0.0
# 在浏览器打开:http://127.0.0.1:8000/docs#/
# 查看接口文档
# 添加一个模块
# module是模块名称,可以设定
fas module-add module
# 添加模块之后,要使模块生效,需要手动在app/main.py文件中注册该路由
# prefix: 该参数定义路由的前缀,每个模块的路由前缀必须是唯一的
from module.router import router as module_router
app.include_router(module_router, prefix="/module", tags=["测试模块"])
# 在当前目录增加一个test.py文件
fas file --filename=test
# 显示所有帮助文档
fas --help
# 某个命令的帮助文档
fas project-init --help
# 查看版本号
fas version
# 如果不设置,则自动使用git中的配置user.name及user.email
fas config --set --author=caiyy --email=caiyy@ibbd.net
# 设置git clone命令的根目录
fas config --set --root-path=/var/www/src
# 可以查看配置信息
fas config
# clone项目
# 项目会自动保存到规范化的目录中:{root-path}/git.ibbd.net/gf/iot-warning
# root-path就是前面设置的配置:fas config --set --root-path=/var/www/src
fas clone git@git.ibbd.net:gf/iot-warning.git
# 代码规范审查
# 审查当前目录
fas check
# 审查指定目录
fas check app
# 在当前目录生成README.md
fas file --filetype=readme --title=测试标题 --desc=描述信息
编码风格遵循PEP8,接口风格参考RESTFul。
重要规则说明:
\n
(vscode编辑器需要配置为LF,而不是CRLF)from typing import Tuple, List, Dict, Set
# 元组
Tuple[int, str, int] # 三个元素的元组
# 列表
List[int] # 元素类型为int的列表
# 字典
Dict[str, int] # key类型为str,value类型为int
# 集合(和列表类型)
Set[int]
BaseModel
的类结构,可以详细定义每个字段的schema。.
├── app
│ ├── __init__.py
│ ├── readme.md # 接口的描述文档
│ ├── main.py # 主入口文件
│ ├── schema.py # 通用schema
│ ├── settings.py # 配置文件
│ ├── dependencies.py #
│ ├── exceptions.py # 异常相关
│ ├── utile.py # 通用的工具函数
│ ├── common # 公共模块
│ │ ├── __init__.py
│ └── module_name # 模块目录,每个模块独立成一个目录
│ ├── __init__.py
│ ├── router.py # 模块路由文件
│ └── schema.py # 模块的schema
├── .vscode # vscode配置
│ ├── settings.json
├── .gitignore
├── README.md # 项目说明文档
├── Dockerfile # Docker
├── requirements.txt # 项目依赖包
模块的路由及其配置文件直接放到模块目录下,而不是将所有路由配置独立到一个目录。
标准化模块,可以使用命令进行快捷添加
PS:趁着假期奉旨宅家,终于把这个工具完善了。