reload-fix: добавил локальный сваггер и обработку перезагрузки страницы фронтом
This commit is contained in:
parent
5cc07c7704
commit
115c652887
3
api/back_static/swagger-ui-bundle.js
Normal file
3
api/back_static/swagger-ui-bundle.js
Normal file
File diff suppressed because one or more lines are too long
3
api/back_static/swagger-ui.css
Normal file
3
api/back_static/swagger-ui.css
Normal file
File diff suppressed because one or more lines are too long
@ -4,8 +4,10 @@ from contextlib import asynccontextmanager
|
|||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
|
|
||||||
from fastapi import FastAPI, Response
|
from fastapi import FastAPI, HTTPException, Response
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
from fastapi.openapi.docs import get_swagger_ui_html
|
||||||
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||||
from sqlalchemy import text
|
from sqlalchemy import text
|
||||||
from sqlalchemy.exc import SQLAlchemyError
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
@ -226,4 +228,43 @@ app.add_middleware(GZipMiddleware, minimum_size=1000, compresslevel=1)
|
|||||||
|
|
||||||
os.makedirs("web", exist_ok=True)
|
os.makedirs("web", exist_ok=True)
|
||||||
app.include_router(api_router, prefix="/api/v1")
|
app.include_router(api_router, prefix="/api/v1")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/docs-local", include_in_schema=False)
|
||||||
|
async def custom_swagger_ui_html():
|
||||||
|
return get_swagger_ui_html(
|
||||||
|
openapi_url=f"{settings.ROOT_PATH}{app.openapi_url}", # Путь к OpenAPI схеме (обычно /openapi.json)
|
||||||
|
title=app.title + " - Swagger UI",
|
||||||
|
oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
|
||||||
|
# Указываем пути к локальным файлам
|
||||||
|
swagger_js_url=f"{settings.ROOT_PATH}/back-static/swagger-ui-bundle.js",
|
||||||
|
swagger_css_url=f"{settings.ROOT_PATH}/back-static/swagger-ui.css",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def sep_static(path, stat_dir):
|
||||||
|
sep_path = path.split("/")
|
||||||
|
index = sep_path.index(stat_dir)
|
||||||
|
return "/".join(sep_path[index:])
|
||||||
|
|
||||||
|
|
||||||
|
class SPAStaticFiles(StaticFiles):
|
||||||
|
async def get_response(self, path: str, scope):
|
||||||
|
try:
|
||||||
|
return await super().get_response(path, scope)
|
||||||
|
except (HTTPException, StarletteHTTPException) as ex:
|
||||||
|
if ex.status_code == 404:
|
||||||
|
if "images" in path:
|
||||||
|
return await super().get_response(sep_static(path, "images"), scope)
|
||||||
|
if "static" in path:
|
||||||
|
return await super().get_response(sep_static(path, "static"), scope)
|
||||||
|
return await super().get_response("index.html", scope)
|
||||||
|
else:
|
||||||
|
raise ex
|
||||||
|
|
||||||
|
|
||||||
|
app.mount("/back-static", StaticFiles(directory="back_static"), name="back-static")
|
||||||
|
app.mount("/", SPAStaticFiles(directory="web", html=True), name="web")
|
||||||
|
|
||||||
app.mount("/", StaticFiles(directory="web", html=True), name="web")
|
app.mount("/", StaticFiles(directory="web", html=True), name="web")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user