利用FastAPI快速开发服务模块

行云流水
2023-03-20 / 0 评论 / 246 阅读 / 正在检测是否收录...

前言

FastAPI 是一个用于构建 Web API 的现代 Python 框架

目录结构

/fastapi
|-- app
|   |-- commands                                ----- 放置一些命令行
|   |   `-- __init__.py
|   |-- exceptions                              ----- 自定义的异常类
|   |   |-- __init__.py
|   |   `-- exception.py
|   |-- http                                    ----- http目录
|   |   |-- api                                 ----- api控制器目录
|   |   |   |-- __init__.py
|   |   |   |-- auth.py                         ----- 登录认证api的控制器
|   |   |   |-- demo.py
|   |   |   `-- users.py
|   |   |-- middleware                          ----- 放置自定义中间件
|   |   |   `-- __init__.py
|   |   |-- __init__.py
|   |   `-- deps.py                             ----- 依赖
|   |-- jobs                                    ----- 调度任务
|   |   |-- __init__.py
|   |   `-- demo_job.py
|   |-- models                                  ----- 模型目录
|   |   |-- __init__.py
|   |   |-- base_model.py                       ----- 定义模型的基类
|   |   `-- user.py
|   |-- providers                               ----- 核心服务提供者
|   |   |-- __init__.py
|   |   |-- app_provider.py                     ----- 注册应用的全局事件、中间件等
|   |   |-- database.py                         ----- 数据库连接
|   |   |-- handle_exception.py                 ----- 异常处理器
|   |   |-- logging_provider.py                 ----- 集成loguru日志系统
|   |   `-- route_provider.py                   ----- 注册路由文件routes/*
|   |-- schemas                                 ----- 数据模型,负责请求和响应资源数据的定义和格式转换
|   |   |-- __init__.py
|   |   `-- user.py
|   |-- services                                ----- 服务层,业务逻辑层
|   |   |-- auth                                ----- 认证相关服务
|   |   |   |-- __init__.py
|   |   |   |-- grant.py                        ----- 认证核心类
|   |   |   |-- hashing.py
|   |   |   |-- jwt_helper.py
|   |   |   |-- oauth2_schema.py
|   |   |   `-- random_code_verifier.py
|   |   `-- __init__.py
|   |-- support                                 ----- 公共方法
|   |   |-- __init__.py
|   |   `-- helper.py
|   `-- __init__.py
|-- bootstrap                                   ----- 启动项
|   |-- __init__.py
|   |-- application.py                          ----- 创建app实例
|   `-- scheduler.py                            ----- 创建调度器实例
|-- config                                      ----- 配置目录
|   |-- auth.py                                 ----- 认证-JWT配置
|   |-- config.py                               ----- app配置
|   |-- database.py                             ----- 数据库配置
|   `-- logging.py                              ----- 日志配置
|-- database
|   `-- migrations                              ----- 初始化SQL
|       `-- 2022_09_07_create_users_table.sql
|-- routes                                      ----- 路由目录
|   |-- __init__.py
|   `-- api.py                                  ----- api路由
|-- storage
|   `-- logs                                    ----- 日志目录
|-- README.md
|-- main.py                                     ----- app/api启动入口
|-- requirements.txt
`-- scheduler.py                                ----- 调度任务启动入口

运行

# 执行初始化SQL:
# 导入database/migrations/2022_09_07_create_users_table.sql

# 主程序
python main.py

# 任务调度器
python scheduler.py

优化

自定义首页

from fastapi.responses import PlainTextResponse

# include_in_schema排除生成文档
@app.get("/", include_in_schema=False, response_class=PlainTextResponse)
async def root():
    return 'hello'

关闭模块


routes/api.py

自定义说明文档

#静态文件
app.mount("/static", StaticFiles(directory="static"), name="static")

description = """
## 说明
- 生成支付订单
"""

app.openapi()["info"] = {
    "title": "abpay api",
    "version": "1.0.0",
    "description": description,
    "contact": {
        "name": "xwzy",
        "email": "1940728253@qq.com",
        "url": "https://me.itbunan.xyz",
    },
    "license": {
        "name": "Apache 2.0",
        "url":  "https://www.apache.org/licenses/LICENSE-2.0.html",
    },
    "x-logo": {
        "url": "/static/img/logo.png"
    }
}

评论 (0)

取消
只有登录/注册用户才可评论