Skip to content

Add Swagger support for closed network environments #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}

Expand Down
4 changes: 4 additions & 0 deletions src/cloudforet/console_api_v2/conf/global_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
40 changes: 30 additions & 10 deletions src/cloudforet/console_api_v2/interface/rest/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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')
Expand All @@ -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)
Loading