fastapi自动生成的接口文档,基于Swagger UI, 但是在内部局域网环境无法访问cdn,因此无法自动生成docs接口文档.
如果要在内网环境生成docs接口文档,需要修改示例代码如下:
from fastapi import FastAPI
import os
from fastapi.openapi.docs import (
get_redoc_html,
get_swagger_ui_html,
get_swagger_ui_oauth2_redirect_html,
)
from fastapi.staticfiles import StaticFiles
app = FastAPI(docs_url=None, redoc_url=None)
root = os.path.abspath(os.path.join(os.path.basename(__file__), "../.."))
print(root)
app.mount("/static", StaticFiles(directory=f"{root}/static"), name="static")
@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",
oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
swagger_js_url="/static/swagger-ui-bundle.js",
swagger_css_url="/stat