-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
71 lines (60 loc) · 2.14 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# !/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@Version : Python 3.12
@Time : 2024/8/7 17:16
@Author : wiesZheng
@Software : PyCharm
"""
import os
from fastapi import FastAPI
from fastapi.openapi.docs import get_redoc_html, get_swagger_ui_html
from starlette.staticfiles import StaticFiles
from app import init_logging, lifespan
from app.commons.openapi import get_stoplight_ui_html
from app.exceptions import register_exceptions_handler
from app.middlewares import register_middlewares
from config import ROOT, settings
app = FastAPI(
title=settings.APP_NAME,
description=f'{settings.APP_NAME} 接口文档',
version=settings.APP_VERSION,
# root_path=AppConfig.APP_ROOT_PATH,
lifespan=lifespan,
)
# 挂载静态文件目录
app.mount('/static', StaticFiles(directory=f'{ROOT}/static'), name='static')
# stoplight_elements doc
@app.get("/openapi", include_in_schema=False)
async def api_documentation():
"""Add stoplight elements api doc. https://dev.to/amal/replacing-fastapis-default-api-docs-with-elements-391d"""
return get_stoplight_ui_html(
openapi_url='/openapi.json',
title=settings.APP_NAME + " - openapi",
# base_path="openapi",
logo='/static/Baguette Bread.png',
stoplight_elements_favicon_url='/static/Baguette Bread.png',
)
@app.get('/docs', include_in_schema=False)
async def custom_swagger_ui_html():
return get_swagger_ui_html(
openapi_url='/openapi.json',
title=settings.APP_NAME + ' - Swagger UI',
swagger_js_url='/static/swagger_ui/swagger-ui-bundle.js',
swagger_css_url='/static/swagger_ui/swagger-ui.css',
swagger_favicon_url='/static/favicon.ico',
)
@app.get('/redoc', include_in_schema=False)
async def custom_redoc_html():
return get_redoc_html(
openapi_url='/openapi.json',
title=settings.APP_NAME + ' - ReDoc',
redoc_js_url='/static/redoc_ui/redoc.standalone.js',
redoc_favicon_url='/static/favicon.ico',
)
# 初始化日志
init_logging(settings.LOGGING_CONF)
# 注册中间件处理方法
register_middlewares(app)
# 注册全局异常处理方法
register_exceptions_handler(app)