Commit 928e7898 authored by uuo00_n's avatar uuo00_n

feat(docs): 自定义Swagger UI使用CDN加载静态资源

禁用FastAPI默认的/docs和/redoc路由,添加自定义Swagger UI端点
从CDN加载静态资源以解决本地网络问题
parent a721cb00
from fastapi import FastAPI
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.middleware.cors import CORSMiddleware
from app.api.v1.router import api_router
from app.core.config import settings
......@@ -25,7 +26,9 @@ app = FastAPI(
{"name": "管理员", "description": "管理员功能(敏感词、分类等)"},
{"name": "对话", "description": "对话与敏感词审计接口"},
],
openapi_url=f"{settings.API_V1_STR}/openapi.json"
openapi_url=f"{settings.API_V1_STR}/openapi.json",
docs_url=None, # 禁用默认的/docs
redoc_url=None # 禁用默认的/redoc
)
# 配置CORS
......@@ -37,6 +40,16 @@ app.add_middleware(
allow_headers=["*"],
)
# 自定义Swagger UI,从CDN加载静态资源,解决本地网络问题
@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui_html():
return get_swagger_ui_html(
openapi_url=app.openapi_url,
title=app.title + " - Swagger UI",
swagger_js_url="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.9.0/swagger-ui-bundle.js",
swagger_css_url="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.9.0/swagger-ui.css",
)
# 注册API路由
app.include_router(api_router, prefix=settings.API_V1_STR)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment