Skip to content

Commit 2b87aff

Browse files
committed
feat: Add Swagger support for closed network environments
1 parent 6eb9a60 commit 2b87aff

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

src/cloudforet/console_api_v2/interface/rest/swagger.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@
33
import json
44
import os
55

6-
from fastapi import APIRouter, Request
6+
from fastapi import APIRouter, Request, FastAPI
77
from fastapi.openapi.docs import get_swagger_ui_html
8+
from fastapi.responses import FileResponse
89

910
from spaceone.core import config
1011
from spaceone.core.cache import cacheable
1112
from spaceone.core.fastapi.api import exception_handler
1213
from cloudforet.console_api_v2.error.swagger import *
1314

14-
1515
_LOGGER = logging.getLogger(__name__)
1616

17+
app = FastAPI()
1718
router = 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')
2124
def _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
4862
async 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)
4.92 KB
Loading

src/cloudforet/console_api_v2/static/swagger_ui.css

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cloudforet/console_api_v2/static/swagger_ui_bundle.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/setup.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,10 @@
3131
'spaceone-api',
3232
'typing-inspect'
3333
],
34+
package_data={
35+
"cloudforet": [
36+
"console_api_v2/static/*",
37+
]
38+
},
3439
zip_safe=False,
3540
)

0 commit comments

Comments
 (0)