| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | """Offline FastAPI documentation support for air-gapped environments.""" |
| 4 | |
| 5 | import pathlib |
| 6 | |
| 7 | from fastapi import FastAPI |
| 8 | from fastapi.openapi.docs import ( |
| 9 | get_swagger_ui_html, |
| 10 | get_swagger_ui_oauth2_redirect_html, |
| 11 | ) |
| 12 | from fastapi.staticfiles import StaticFiles |
| 13 | |
| 14 | from vllm.logger import init_logger |
| 15 | |
| 16 | logger = init_logger(__name__) |
| 17 | |
| 18 | |
| 19 | def attach_router(app: FastAPI) -> None: |
| 20 | """Attach offline docs router if enabled via args.""" |
| 21 | args = getattr(app.state, "args", None) |
| 22 | if args is None or not getattr(args, "enable_offline_docs", False): |
| 23 | return |
| 24 | |
| 25 | static_dir = pathlib.Path(__file__).parent / "static" |
| 26 | |
| 27 | if not static_dir.exists(): |
| 28 | logger.warning( |
| 29 | "Static directory not found at %s. Offline docs will not be available.", |
| 30 | static_dir, |
| 31 | ) |
| 32 | return |
| 33 | |
| 34 | app.mount("/static", StaticFiles(directory=str(static_dir)), name="static") |
| 35 | |
| 36 | @app.get("/docs", include_in_schema=False) |
| 37 | async def custom_swagger_ui_html(): |
| 38 | return get_swagger_ui_html( |
| 39 | openapi_url=app.openapi_url, |
| 40 | title=app.title + " - Swagger UI", |
| 41 | oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url, |
| 42 | swagger_js_url="/static/swagger-ui-bundle.js", |
| 43 | swagger_css_url="/static/swagger-ui.css", |
| 44 | ) |
| 45 | |
| 46 | @app.get(app.swagger_ui_oauth2_redirect_url, include_in_schema=False) |
| 47 | async def swagger_ui_redirect(): |
| 48 | return get_swagger_ui_oauth2_redirect_html() |
| 49 | |
| 50 | logger.info("Offline documentation enabled with vendored static assets") |