Skip to content

Commit

Permalink
feat: Add option for using static Swagger UI
Browse files Browse the repository at this point in the history
  • Loading branch information
lhhyung committed Jan 17, 2025
1 parent 89aa4ad commit 8033927
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 29 deletions.
11 changes: 9 additions & 2 deletions 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 All @@ -32,4 +39,4 @@ RUN pip install --upgrade spaceone-api
EXPOSE ${SPACEONE_PORT}

ENTRYPOINT ["spaceone"]
CMD ["run", "rest-server", "cloudforet.console_api_v2", "-m", "/opt"]
CMD ["run", "rest-server", "cloudforet.console_api_v2", "-m", "/opt", "--host", "0.0.0.0"]
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
42 changes: 24 additions & 18 deletions src/cloudforet/console_api_v2/interface/rest/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import os

from fastapi import APIRouter, Request, FastAPI
from fastapi import APIRouter, Request
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.responses import FileResponse

Expand All @@ -14,11 +14,8 @@

_LOGGER = logging.getLogger(__name__)

app = FastAPI()
router = APIRouter(include_in_schema=False)

SWAGGER_UI_PATH = os.path.join(os.path.dirname(__file__), f"../../static")


@cacheable(key='openapi-json:{service}', alias='local')
def _get_openapi_json_data(service):
Expand All @@ -38,19 +35,28 @@ 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,
swagger_css_url="/static/swagger_ui.css",
swagger_js_url="/static/swagger_ui_bundle.js",
swagger_favicon_url="/static/favicon.png",
)


def _get_swagger_ui_files(filename):
file_path = os.path.join(SWAGGER_UI_PATH, filename)
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}')
Expand All @@ -71,4 +77,4 @@ async def openapi_json(service: str):

@router.get("/static/{filename:path}")
async def swagger_ui(filename: str):
return _get_swagger_ui_files(filename)
return _get_static_swagger_ui_file(filename)
Binary file removed src/cloudforet/console_api_v2/static/favicon.png
Binary file not shown.
2 changes: 0 additions & 2 deletions src/cloudforet/console_api_v2/static/swagger_ui.css

This file was deleted.

2 changes: 0 additions & 2 deletions src/cloudforet/console_api_v2/static/swagger_ui_bundle.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,5 @@
'spaceone-api',
'typing-inspect'
],
package_data={
"cloudforet": [
"console_api_v2/static/*",
]
},
zip_safe=False,
)

0 comments on commit 8033927

Please sign in to comment.