/
misha_dev
/
game_guild_backend
Обзор
Документация
Войти
/
misha_dev
/
game_guild_backend
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
fastapi-application/create_fastapi_app.py
71 строка
2 KB
misha-dev
feat: categories search, error language
30 мар 2025, 20:17
30 мар 2025, 20:17
836d927
Код
Авторство
О чём код?
from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.responses import ORJSONResponse from fastapi.openapi.docs import ( get_redoc_html, get_swagger_ui_html, get_swagger_ui_oauth2_redirect_html, ) from fastapi.middleware.cors import CORSMiddleware from core.models import db_helper @asynccontextmanager async def lifespan(app: FastAPI): # startup yield # shutdown await db_helper.dispose() def register_static_docs_routes(app: FastAPI): @app.get("/docs", include_in_schema=False) async def custom_swagger_ui_html(): return get_swagger_ui_html( openapi_url=app.openapi_url, title=app.title + " - Swagger UI", oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url, swagger_js_url="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js", swagger_css_url="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css", ) @app.get(app.swagger_ui_oauth2_redirect_url, include_in_schema=False) async def swagger_ui_redirect(): return get_swagger_ui_oauth2_redirect_html() @app.get("/redoc", include_in_schema=False) async def redoc_html(): return get_redoc_html( openapi_url=app.openapi_url, title=app.title + " - ReDoc", redoc_js_url="https://unpkg.com/redoc@next/bundles/redoc.standalone.js", ) def create_app( create_custom_static_urls: bool = False, ) -> FastAPI: app = FastAPI( default_response_class=ORJSONResponse, lifespan=lifespan, docs_url=None if create_custom_static_urls else "/docs", redoc_url=None if create_custom_static_urls else "/redoc", ) origins = ["*"] app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], expose_headers=["*"], ) if create_custom_static_urls: register_static_docs_routes(app) return app