Configure self-hosting JavaScript and CSS for docs

This commit is contained in:
grossmj 2024-12-19 16:54:11 +07:00
parent 648ae231ef
commit 0ee73605c0
No known key found for this signature in database
GPG Key ID: 1E7DD6DBB53FF3D7
5 changed files with 1826 additions and 6 deletions

View File

@ -27,20 +27,20 @@ router = APIRouter()
templates = Jinja2Templates(directory=os.path.join("gns3server", "templates"))
@router.get("/")
@router.get("/", include_in_schema=False)
async def root():
return RedirectResponse("/static/web-ui/bundled", status_code=308) # permanent redirect
@router.get("/debug", response_class=HTMLResponse, deprecated=True)
@router.get("/debug", response_class=HTMLResponse, deprecated=True, include_in_schema=False)
def debug(request: Request):
kwargs = {"request": request, "gns3_version": __version__, "gns3_host": request.client.host}
return templates.TemplateResponse("index.html", kwargs)
@router.get("/static/web-ui/{file_path:path}", description="Web user interface")
@router.get("/static/web-ui/{file_path:path}", description="Web user interface", include_in_schema=False)
async def web_ui(file_path: str):
file_path = os.path.normpath(file_path).strip("/")

View File

@ -19,15 +19,20 @@
FastAPI app
"""
import time
from fastapi import FastAPI, Request, HTTPException, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi.exceptions import RequestValidationError
from fastapi.staticfiles import StaticFiles
from sqlalchemy.exc import SQLAlchemyError
from uvicorn.main import Server as UvicornServer
from fastapi.openapi.docs import (
get_redoc_html,
get_swagger_ui_html,
get_swagger_ui_oauth2_redirect_html,
)
from gns3server.controller.controller_error import (
ControllerError,
ControllerNotFoundError,
@ -51,7 +56,11 @@ log = logging.getLogger(__name__)
def get_application() -> FastAPI:
application = FastAPI(
title="GNS3 controller API", description="This page describes the public controller API for GNS3", version="v3"
title="GNS3 controller API",
description="This page describes the public controller API for GNS3",
version="v3",
docs_url=None,
redoc_url=None
)
application.add_middleware(
@ -66,6 +75,7 @@ def get_application() -> FastAPI:
application.add_event_handler("shutdown", tasks.create_shutdown_handler(application))
application.include_router(index.router, tags=["Index"])
application.include_router(controller.router, prefix="/v3")
application.mount("/static", StaticFiles(packages=[('gns3server', 'static')]), name="static")
application.mount("/v3/compute", compute_api, name="compute")
return application
@ -85,6 +95,29 @@ def handle_exit(*args, **kwargs):
UvicornServer.handle_exit = handle_exit
# Configure self-hosting JavaScript and CSS for docs
@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="/static/swagger-ui.css",
)
@app.get(app.swagger_ui_oauth2_redirect_url, include_in_schema=False)
async def swagger_ui_redirect():
return get_swagger_ui_oauth2_redirect_html()
@app.get("/redoc", include_in_schema=False)
async def redoc_html():
return get_redoc_html(
openapi_url=app.openapi_url,
title=app.title + " - ReDoc",
redoc_js_url="/static/redoc.standalone.js",
)
@app.exception_handler(ControllerError)
async def controller_error_handler(request: Request, exc: ControllerError):

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long