From cdadd38cf7ef8f63a55c208065db92f7bb90dc98 Mon Sep 17 00:00:00 2001 From: Yuki <60097976+binaryYuki@users.noreply.github.com> Date: Mon, 10 Jun 2024 22:26:46 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8Ffeat:=20block=20access=20to?= =?UTF-8?q?=20openapi=20references=20while=20running=20under=20fastapi=20(?= =?UTF-8?q?#1849)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - block fastapi openapi reference(swagger and redoc) routes --- shared_utils/fastapi_server.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/shared_utils/fastapi_server.py b/shared_utils/fastapi_server.py index b30a5aaa..7b06fab9 100644 --- a/shared_utils/fastapi_server.py +++ b/shared_utils/fastapi_server.py @@ -198,13 +198,21 @@ def start_app(app_block, CONCURRENT_COUNT, AUTHENTICATION, PORT, SSL_KEYFILE, SS fastapi_app = FastAPI(lifespan=app_lifespan) fastapi_app.mount(CUSTOM_PATH, gradio_app) - # --- --- favicon --- --- + # --- --- favicon and block fastapi api reference routes --- --- + from starlette.responses import JSONResponse if CUSTOM_PATH != '/': from fastapi.responses import FileResponse @fastapi_app.get("/favicon.ico") async def favicon(): return FileResponse(app_block.favicon_path) + @fastapi_app.middleware("http") + async def middleware(request: Request, call_next): + if request.scope['path'] == "/docs" or request.scope['path'] == "/redoc" or request.scope['path'] == "/openapi.json": + return JSONResponse(status_code=404, content={"message": "Not Found"}) + response = await call_next(request) + return response + # --- --- uvicorn.Config --- --- ssl_keyfile = None if SSL_KEYFILE == "" else SSL_KEYFILE ssl_certfile = None if SSL_CERTFILE == "" else SSL_CERTFILE @@ -220,7 +228,7 @@ def start_app(app_block, CONCURRENT_COUNT, AUTHENTICATION, PORT, SSL_KEYFILE, SS ) server = Server(config) url_host_name = "localhost" if server_name == "0.0.0.0" else server_name - if ssl_keyfile is not None: + if ssl_keyfile is not None: if ssl_certfile is None: raise ValueError( "ssl_certfile must be provided if ssl_keyfile is provided."