fastapi swagger
app.include_router(api_router)
# Add documentation endpoints only in test environment
if DEPLOY_API_ENVIROMENT == "test":
logger.info("Test environment detected - Full documentation enabled with authentication")
# Custom protected docs endpoints for TEST environment
@app.get("/v1/doc/api-docs", include_in_schema=False)
async def get_swagger_ui_documentation(
request: Request,
username: str = Depends(get_current_user)
):
return get_swagger_ui_html(
openapi_url="/v1/doc/openapi.json",
title=app.title + " - Swagger UI (Test Environment)",
swagger_js_url="https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/swagger-ui/5.9.0/swagger-ui-bundle.min.js",
swagger_css_url="https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/swagger-ui/5.9.0/swagger-ui.min.css",
)
@app.get("/v1/doc/api-redoc", include_in_schema=False)
async def get_redoc_documentation(
request: Request,
username: str = Depends(get_current_user)
):
return get_redoc_html(
openapi_url="/v1/doc/openapi.json",
title=app.title + " - ReDoc (Test Environment)",
redoc_js_url="https://s4.zstatic.net/npm/redoc@2.1.3/bundles/redoc.standalone.js",
)
@app.get("/v1/doc/openapi.json", include_in_schema=False)
async def get_openapi_json(
request: Request,
username: str = Depends(get_current_user)
):
from fastapi.openapi.utils import get_openapi
return get_openapi(
title=app.title,
version=app.version,
description=app.description,
routes=app.routes,
)
else:
logger.info("Production environment detected - All documentation disabled")
ref: https://developer.aliyun.com/article/1426614
htps://fonts.font.im/css?family=Montserrat:300,400,700
https://blog.csdn.net/h1773655323/article/details/142098108