33import json
44import os
55
6- from fastapi import APIRouter , Request
6+ from fastapi import APIRouter , Request , FastAPI
77from fastapi .openapi .docs import get_swagger_ui_html
8+ from fastapi .responses import FileResponse
89
910from spaceone .core import config
1011from spaceone .core .cache import cacheable
1112from spaceone .core .fastapi .api import exception_handler
1213from cloudforet .console_api_v2 .error .swagger import *
1314
14-
1515_LOGGER = logging .getLogger (__name__ )
1616
17+ app = FastAPI ()
1718router = APIRouter (include_in_schema = False )
1819
20+ SWAGGER_UI_PATH = os .path .join (os .path .dirname (__file__ ), f"../../static" )
21+
1922
2023@cacheable (key = 'openapi-json:{service}' , alias = 'local' )
2124def _get_openapi_json_data (service ):
@@ -40,9 +43,20 @@ def _get_swagger_ui_html(request: Request, service):
4043 title = f'{ service .title ().replace ("-" , " " )} API' + ' - Swagger UI' ,
4144 oauth2_redirect_url = request .app .swagger_ui_oauth2_redirect_url ,
4245 init_oauth = request .app .swagger_ui_init_oauth ,
46+ swagger_css_url = "/static/swagger_ui.css" ,
47+ swagger_js_url = "/static/swagger_ui_bundle.js" ,
48+ swagger_favicon_url = "/static/favicon.png" ,
4349 )
4450
4551
52+ def _get_swagger_ui_files (filename ):
53+ file_path = os .path .join (SWAGGER_UI_PATH , filename )
54+ if os .path .exists (file_path ):
55+ return FileResponse (file_path )
56+ _LOGGER .error (f'Swagger UI file not found { filename } ' )
57+ raise ERROR_NOT_FOUND (key = 'file' , value = filename )
58+
59+
4660@router .get ('/{service}/docs' )
4761@exception_handler
4862async def docs (request : Request , service : str ):
@@ -55,6 +69,6 @@ async def openapi_json(service: str):
5569 return _get_openapi_json_data (service )
5670
5771
58-
59-
60-
72+ @ router . get ( "/static/{filename:path}" )
73+ async def swagger_ui ( filename : str ):
74+ return _get_swagger_ui_files ( filename )
0 commit comments