sipi-web/sipi-app/main.py

52 lines
1.3 KiB
Python

from contextlib import asynccontextmanager
from fastapi.openapi.docs import get_swagger_ui_html
from starlette.staticfiles import StaticFiles
from settings import settings, BASE_DIR
from models import db_helper
from api import router as api_router
from views import router as web_router
import uvicorn
from fastapi import FastAPI
from fastapi.responses import ORJSONResponse
@asynccontextmanager
async def lifespan(app: FastAPI):
yield
await db_helper.dispose()
main_app = FastAPI(
default_response_class=ORJSONResponse,
lifespan=lifespan,
docs_url=None,
)
main_app.include_router(api_router)
main_app.include_router(web_router)
main_app.mount("/static", StaticFiles(directory=BASE_DIR / "views" / "static"), name="static")
@main_app.get("/docs", include_in_schema=False)
async def custom_swagger_ui_html():
return get_swagger_ui_html(
openapi_url=main_app.openapi_url,
title="My API",
oauth2_redirect_url=main_app.swagger_ui_oauth2_redirect_url,
swagger_js_url="/static/swagger/swagger-ui-bundle.js",
swagger_css_url="/static/swagger/swagger-ui.css",
swagger_favicon_url="/static/swagger/favicon.png",
)
if __name__ == "__main__":
uvicorn.run(
"main:main_app",
host=settings.run.host,
port=settings.run.port,
reload=settings.run.reload,
)