diff --git a/Dockerfile b/Dockerfile index 6b13de1..725901b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,7 @@ ENV CONF_DIR=/etc/spaceone ENV LOG_DIR=/var/log/spaceone ENV GIT_DIR=/tmp/git ENV OPENAPI_JSON_DIR=/opt/openapi +ENV SWAGGER_UI_DIR=/opt/swagger-ui ENV PACKAGE_VERSION=$PACKAGE_VERSION COPY pkg/pip_requirements.txt pip_requirements.txt @@ -16,12 +17,18 @@ RUN pip install --upgrade pip && \ pip install --upgrade -r pip_requirements.txt RUN apt-get update && apt-get install -y git -RUN mkdir -p ${OPENAPI_JSON_DIR} +RUN mkdir -p ${OPENAPI_JSON_DIR} ${SWAGGER_UI_DIR} WORKDIR ${GIT_DIR} RUN git clone --branch ${BRANCH_NAME} https://github.com/cloudforet-io/api.git RUN cp -r api/dist/openapi/* ${OPENAPI_JSON_DIR} RUN rm -rf ${GIT_DIR} +WORKDIR ${SWAGGER_UI_DIR} +RUN wget -q https://cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui-bundle.js \ + && wget -q https://cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui.css \ + && wget -q https://cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui.css.map \ + && wget -q https://fastapi.tiangolo.com/img/favicon.png + COPY src ${SRC_DIR} WORKDIR ${SRC_DIR} diff --git a/src/cloudforet/console_api_v2/conf/global_conf.py b/src/cloudforet/console_api_v2/conf/global_conf.py index b6d83e0..5040a9e 100644 --- a/src/cloudforet/console_api_v2/conf/global_conf.py +++ b/src/cloudforet/console_api_v2/conf/global_conf.py @@ -54,6 +54,10 @@ "/opt/openapi/cloudforet/api/alert_manager/v1/*.json", ] +# Swagger UI Settings +STATIC_SWAGGER_UI = False +STATIC_SWAGGER_UI_DIR = "/opt/swagger-ui" + UVICORN_OPTIONS = {"factory": True} LOG = { diff --git a/src/cloudforet/console_api_v2/interface/rest/swagger.py b/src/cloudforet/console_api_v2/interface/rest/swagger.py index cc6411c..c8bfe56 100644 --- a/src/cloudforet/console_api_v2/interface/rest/swagger.py +++ b/src/cloudforet/console_api_v2/interface/rest/swagger.py @@ -5,13 +5,13 @@ from fastapi import APIRouter, Request from fastapi.openapi.docs import get_swagger_ui_html +from fastapi.responses import FileResponse from spaceone.core import config from spaceone.core.cache import cacheable from spaceone.core.fastapi.api import exception_handler from cloudforet.console_api_v2.error.swagger import * - _LOGGER = logging.getLogger(__name__) router = APIRouter(include_in_schema=False) @@ -35,12 +35,32 @@ def _get_openapi_json_data(service): @cacheable(key='swagger-ui-html:{service}', alias='local') def _get_swagger_ui_html(request: Request, service): - return get_swagger_ui_html( - openapi_url=f'/{service}/openapi.json', - title=f'{service.title().replace("-", " ")} API' + ' - Swagger UI', - oauth2_redirect_url=request.app.swagger_ui_oauth2_redirect_url, - init_oauth=request.app.swagger_ui_init_oauth, - ) + static_swagger_ui = config.get_global('STATIC_SWAGGER_UI', False) + + swagger_ui_options = { + "openapi_url": f'/{service}/openapi.json', + "title": f'{service.title().replace("-", " ")} API - Swagger UI', + "oauth2_redirect_url": request.app.swagger_ui_oauth2_redirect_url, + "init_oauth": request.app.swagger_ui_init_oauth, + } + + if static_swagger_ui: + swagger_ui_options.update({ + "swagger_css_url": "/static/swagger-ui.css", + "swagger_js_url": "/static/swagger-ui-bundle.js", + "swagger_favicon_url": "/static/favicon.png", + }) + + return get_swagger_ui_html(**swagger_ui_options) + + +def _get_static_swagger_ui_file(filename: str): + swagger_ui_file_dir = config.get_global('STATIC_SWAGGER_UI_DIR') + file_path = os.path.join(swagger_ui_file_dir, filename) + if os.path.exists(file_path): + return FileResponse(file_path) + _LOGGER.error(f'Swagger UI file not found {filename}') + raise ERROR_NOT_FOUND(key='file', value=filename) @router.get('/{service}/docs') @@ -55,6 +75,6 @@ async def openapi_json(service: str): return _get_openapi_json_data(service) - - - +@router.get("/static/{filename:path}") +async def swagger_ui(filename: str): + return _get_static_swagger_ui_file(filename)