Skip to content

Commit 47a6ab1

Browse files
committed
static Swagger files
1 parent a5f945e commit 47a6ab1

File tree

4 files changed

+265
-3
lines changed

4 files changed

+265
-3
lines changed

app/main.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
import pydantic
77
import sentry_sdk
88
import uvicorn
9-
from fastapi import FastAPI, Request, Response
9+
from fastapi import FastAPI, Request, Response, openapi
1010
from fastapi.middleware.cors import CORSMiddleware
1111
from fastapi.middleware.gzip import GZipMiddleware
1212
from fastapi.responses import JSONResponse
13+
from fastapi.staticfiles import StaticFiles
1314
from scout_apm.async_.starlette import ScoutMiddleware
1415
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
1516

@@ -35,8 +36,8 @@
3536
" Project page: https://github.com/ExpDev07/coronavirus-tracker-api."
3637
),
3738
version="2.0.3",
38-
docs_url="/",
39-
redoc_url="/docs",
39+
docs_url=None,
40+
redoc_url=None,
4041
on_startup=[setup_client_session],
4142
on_shutdown=[teardown_client_session],
4243
)
@@ -108,6 +109,34 @@ async def handle_validation_error(
108109
# Include routers.
109110
APP.include_router(V1, prefix="", tags=["v1"])
110111
APP.include_router(V2, prefix="/v2", tags=["v2"])
112+
APP.mount("/static", StaticFiles(directory="static"), name="static")
113+
114+
# ##############
115+
# Swagger/Redocs
116+
# ##############
117+
118+
119+
@APP.get("/", include_in_schema=False)
120+
async def custom_swagger_ui_html():
121+
return openapi.docs.get_swagger_ui_html(
122+
openapi_url=APP.openapi_url,
123+
title=f"{APP.title} - Swagger UI",
124+
oauth2_redirect_url=APP.swagger_ui_oauth2_redirect_url,
125+
swagger_js_url="/static/swagger-ui-bundle.js",
126+
swagger_css_url="/static/swagger-ui.css",
127+
)
128+
129+
130+
@APP.get(APP.swagger_ui_oauth2_redirect_url, include_in_schema=False)
131+
async def swagger_ui_redirect():
132+
return openapi.docs.get_swagger_ui_oauth2_redirect_html()
133+
134+
135+
@APP.get("/docs", include_in_schema=False)
136+
async def redoc_html():
137+
return openapi.docs.get_redoc_html(
138+
openapi_url=APP.openapi_url, title=f"{APP.title} - ReDoc", redoc_js_url="/static/redoc.standalone.js",
139+
)
111140

112141

113142
# Running of app.

static/redoc.standalone.js

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

static/swagger-ui-bundle.js

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

static/swagger-ui.css

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

0 commit comments

Comments
 (0)