Skip to content

Commit 5f0fe1c

Browse files
committed
update:将swagger-ui的接口增加voiceprint前缀,方便反向代理
1 parent bf2a60d commit 5f0fe1c

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

app/application.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from fastapi.security import HTTPBearer
33
from fastapi.middleware.cors import CORSMiddleware
44
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
57

68
from .api.v1.api import api_router
79

@@ -17,8 +19,8 @@ def create_app() -> FastAPI:
1719
title="3D-Speaker 声纹识别API",
1820
description="基于3D-Speaker的声纹注册与识别服务",
1921
version="2.0.0",
20-
docs_url="/voiceprint/docs",
21-
redoc_url="/voiceprint/redoc",
22+
docs_url=None, # 禁用默认的docs路径
23+
redoc_url=None, # 禁用默认的redoc路径
2224
)
2325

2426
# 添加CORS中间件
@@ -33,6 +35,41 @@ def create_app() -> FastAPI:
3335
# 注册API路由
3436
app.include_router(api_router, prefix="/voiceprint")
3537

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+
3673
# 根路径
3774
@app.get("/", include_in_schema=False)
3875
def root():

0 commit comments

Comments
 (0)