feat: 加载本地文档前端静态资源

This commit is contained in:
martsforever
2026-03-27 14:08:47 +08:00
parent 107981a1f7
commit a262a95003
4 changed files with 1926 additions and 4 deletions
+73 -4
View File
@@ -1,12 +1,81 @@
import logging
from typing import List, Union
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.openapi.docs import get_swagger_ui_oauth2_redirect_html, get_redoc_html, get_swagger_ui_html
from fastapi.responses import RedirectResponse from fastapi.responses import RedirectResponse
from langchain.chat_models import init_chat_model
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnableGenerator
from langchain_openai import ChatOpenAI
from langserve import add_routes from langserve import add_routes
app = FastAPI() from pydantic import BaseModel, Field
from starlette.staticfiles import StaticFiles
app = FastAPI(
docs_url=None, # 禁用默认 Swagger
redoc_url=None, # 禁用默认 ReDoc
)
app.mount("/static", StaticFiles(directory="static"), name="static")
# 自定义 Swagger 页面(使用本地资源)
@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui():
"""
提供自定义的 Swagger UI 文档页面
Returns:
Swagger UI HTML 页面,使用本地静态资源加载
"""
return get_swagger_ui_html(
openapi_url=app.openapi_url,
title=app.title + " - Swagger UI",
oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
swagger_js_url="/static/swagger-ui-bundle.min.js",
swagger_css_url="/static/swagger-ui.min.css",
)
@app.get(app.swagger_ui_oauth2_redirect_url, include_in_schema=False)
async def swagger_ui_redirect():
"""
处理 Swagger UI 的 OAuth2 重定向回调
Returns:
OAuth2 重定向 HTML 响应
"""
return get_swagger_ui_oauth2_redirect_html()
@app.get("/redoc", include_in_schema=False)
async def redoc_html():
"""
提供自定义的 ReDoc 文档页面
Returns:
ReDoc HTML 页面,使用本地静态资源加载
"""
return get_redoc_html(
openapi_url=app.openapi_url,
title=app.title + " - ReDoc",
redoc_js_url="/static/redoc.standalone.js",
)
@app.get("/") @app.get("/")
async def redirect_root_to_docs() -> RedirectResponse: async def redirect_root_to_docs():
"""
将根路径请求重定向到 Swagger 文档页面
Returns:
重定向响应到 /docs 路径
"""
return RedirectResponse("/docs") return RedirectResponse("/docs")
# Edit this to add the chain you want to add
# add_routes(app, NotImplemented)
if __name__ == "__main__": if __name__ == "__main__":
import uvicorn import uvicorn
uvicorn.run(app, host="localhost", port=8000) uvicorn.run(app, host="localhost", port=8000)
File diff suppressed because one or more lines are too long
+20
View File
File diff suppressed because one or more lines are too long
+1
View File
File diff suppressed because one or more lines are too long