mirror of
https://github.com/GNS3/gns3-server
synced 2025-01-13 17:40:54 +00:00
Merge pull request #2456 from GNS3/feature/self-hosted-static-files
Configure self-hosting JavaScript and CSS for docs
This commit is contained in:
commit
c9694a4e1d
@ -27,20 +27,20 @@ router = APIRouter()
|
|||||||
templates = Jinja2Templates(directory=os.path.join("gns3server", "templates"))
|
templates = Jinja2Templates(directory=os.path.join("gns3server", "templates"))
|
||||||
|
|
||||||
|
|
||||||
@router.get("/")
|
@router.get("/", include_in_schema=False)
|
||||||
async def root():
|
async def root():
|
||||||
|
|
||||||
return RedirectResponse("/static/web-ui/bundled", status_code=308) # permanent redirect
|
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):
|
def debug(request: Request):
|
||||||
|
|
||||||
kwargs = {"request": request, "gns3_version": __version__, "gns3_host": request.client.host}
|
kwargs = {"request": request, "gns3_version": __version__, "gns3_host": request.client.host}
|
||||||
return templates.TemplateResponse("index.html", kwargs)
|
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):
|
async def web_ui(file_path: str):
|
||||||
|
|
||||||
file_path = os.path.normpath(file_path).strip("/")
|
file_path = os.path.normpath(file_path).strip("/")
|
||||||
|
@ -19,15 +19,20 @@
|
|||||||
FastAPI app
|
FastAPI app
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import time
|
|
||||||
|
|
||||||
from fastapi import FastAPI, Request, HTTPException, status
|
from fastapi import FastAPI, Request, HTTPException, status
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse
|
||||||
from fastapi.exceptions import RequestValidationError
|
from fastapi.exceptions import RequestValidationError
|
||||||
|
from fastapi.staticfiles import StaticFiles
|
||||||
from sqlalchemy.exc import SQLAlchemyError
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
from uvicorn.main import Server as UvicornServer
|
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 (
|
from gns3server.controller.controller_error import (
|
||||||
ControllerError,
|
ControllerError,
|
||||||
ControllerNotFoundError,
|
ControllerNotFoundError,
|
||||||
@ -51,7 +56,11 @@ log = logging.getLogger(__name__)
|
|||||||
def get_application() -> FastAPI:
|
def get_application() -> FastAPI:
|
||||||
|
|
||||||
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(
|
application.add_middleware(
|
||||||
@ -66,6 +75,7 @@ def get_application() -> FastAPI:
|
|||||||
application.add_event_handler("shutdown", tasks.create_shutdown_handler(application))
|
application.add_event_handler("shutdown", tasks.create_shutdown_handler(application))
|
||||||
application.include_router(index.router, tags=["Index"])
|
application.include_router(index.router, tags=["Index"])
|
||||||
application.include_router(controller.router, prefix="/v3")
|
application.include_router(controller.router, prefix="/v3")
|
||||||
|
application.mount("/static", StaticFiles(packages=[('gns3server', 'static')]), name="static")
|
||||||
application.mount("/v3/compute", compute_api, name="compute")
|
application.mount("/v3/compute", compute_api, name="compute")
|
||||||
|
|
||||||
return application
|
return application
|
||||||
@ -85,6 +95,29 @@ def handle_exit(*args, **kwargs):
|
|||||||
|
|
||||||
UvicornServer.handle_exit = handle_exit
|
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)
|
@app.exception_handler(ControllerError)
|
||||||
async def controller_error_handler(request: Request, exc: ControllerError):
|
async def controller_error_handler(request: Request, exc: ControllerError):
|
||||||
|
1782
gns3server/static/redoc.standalone.js
Normal file
1782
gns3server/static/redoc.standalone.js
Normal file
File diff suppressed because one or more lines are too long
2
gns3server/static/swagger-ui-bundle.js
Normal file
2
gns3server/static/swagger-ui-bundle.js
Normal file
File diff suppressed because one or more lines are too long
3
gns3server/static/swagger-ui.css
Normal file
3
gns3server/static/swagger-ui.css
Normal file
File diff suppressed because one or more lines are too long
@ -31,6 +31,9 @@ ALLOWED_CONTROLLER_ENDPOINTS = [
|
|||||||
("/", "GET"),
|
("/", "GET"),
|
||||||
("/debug", "GET"),
|
("/debug", "GET"),
|
||||||
("/static/web-ui/{file_path:path}", "GET"),
|
("/static/web-ui/{file_path:path}", "GET"),
|
||||||
|
("/docs", "GET"),
|
||||||
|
("/docs/oauth2-redirect", "GET"),
|
||||||
|
("/redoc", "GET"),
|
||||||
("/v3/version", "GET"),
|
("/v3/version", "GET"),
|
||||||
("/v3/version", "POST"),
|
("/v3/version", "POST"),
|
||||||
("/v3/access/users/login", "POST"),
|
("/v3/access/users/login", "POST"),
|
||||||
|
Loading…
Reference in New Issue
Block a user