2
2
from fastapi .security import HTTPBearer
3
3
from fastapi .middleware .cors import CORSMiddleware
4
4
from fastapi .responses import RedirectResponse
5
+ from fastapi .openapi .docs import get_swagger_ui_html , get_redoc_html
6
+ from fastapi .openapi .utils import get_openapi
5
7
6
8
from .api .v1 .api import api_router
7
9
@@ -17,8 +19,8 @@ def create_app() -> FastAPI:
17
19
title = "3D-Speaker 声纹识别API" ,
18
20
description = "基于3D-Speaker的声纹注册与识别服务" ,
19
21
version = "2.0.0" ,
20
- docs_url = "/voiceprint/docs" ,
21
- redoc_url = "/voiceprint/redoc" ,
22
+ docs_url = None , # 禁用默认的docs路径
23
+ redoc_url = None , # 禁用默认的redoc路径
22
24
)
23
25
24
26
# 添加CORS中间件
@@ -33,6 +35,41 @@ def create_app() -> FastAPI:
33
35
# 注册API路由
34
36
app .include_router (api_router , prefix = "/voiceprint" )
35
37
38
+ # 自定义OpenAPI文档路径
39
+ @app .get ("/voiceprint/openapi.json" , include_in_schema = False )
40
+ async def custom_openapi ():
41
+ """自定义OpenAPI JSON路径"""
42
+ if app .openapi_schema :
43
+ return app .openapi_schema
44
+ app .openapi_schema = get_openapi (
45
+ title = app .title ,
46
+ version = app .version ,
47
+ description = app .description ,
48
+ routes = app .routes ,
49
+ )
50
+ return app .openapi_schema
51
+
52
+ # 自定义Swagger UI文档
53
+ @app .get ("/voiceprint/docs" , include_in_schema = False )
54
+ async def custom_swagger_ui_html ():
55
+ """自定义Swagger UI文档页面"""
56
+ return get_swagger_ui_html (
57
+ openapi_url = "/voiceprint/openapi.json" ,
58
+ title = app .title + " - Swagger UI" ,
59
+ swagger_js_url = "https://cdn.jsdelivr.net/npm/[email protected] /swagger-ui-bundle.js" ,
60
+ swagger_css_url = "https://cdn.jsdelivr.net/npm/[email protected] /swagger-ui.css" ,
61
+ )
62
+
63
+ # 自定义ReDoc文档
64
+ @app .get ("/voiceprint/redoc" , include_in_schema = False )
65
+ async def custom_redoc_html ():
66
+ """自定义ReDoc文档页面"""
67
+ return get_redoc_html (
68
+ openapi_url = "/voiceprint/openapi.json" ,
69
+ title = app .title + " - ReDoc" ,
70
+ redoc_js_url = "https://cdn.jsdelivr.net/npm/[email protected] /bundles/redoc.standalone.js" ,
71
+ )
72
+
36
73
# 根路径
37
74
@app .get ("/" , include_in_schema = False )
38
75
def root ():
0 commit comments