sipi-web/sipi-app/main.py

52 lines
1.3 KiB
Python
Raw Normal View History

2024-07-11 16:01:26 +00:00
from contextlib import asynccontextmanager
2024-08-06 07:05:03 +00:00
from fastapi.openapi.docs import get_swagger_ui_html
from starlette.staticfiles import StaticFiles
2024-08-06 07:05:03 +00:00
from settings import settings, BASE_DIR
from models import db_helper
2024-07-11 16:01:26 +00:00
from api import router as api_router
from views import router as web_router
2024-07-11 16:01:26 +00:00
import uvicorn
from fastapi import FastAPI
2024-07-12 11:49:52 +00:00
from fastapi.responses import ORJSONResponse
2024-07-11 16:01:26 +00:00
2024-07-11 16:01:26 +00:00
@asynccontextmanager
async def lifespan(app: FastAPI):
yield
await db_helper.dispose()
2024-07-11 16:01:26 +00:00
main_app = FastAPI(
2024-07-12 11:49:52 +00:00
default_response_class=ORJSONResponse,
2024-07-11 16:01:26 +00:00
lifespan=lifespan,
2024-08-06 07:05:03 +00:00
docs_url=None,
2024-07-11 16:01:26 +00:00
)
main_app.include_router(api_router)
main_app.include_router(web_router)
2024-07-11 16:01:26 +00:00
2024-08-06 07:05:03 +00:00
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",
)
2024-07-11 16:01:26 +00:00
if __name__ == "__main__":
uvicorn.run(
"main:main_app",
host=settings.run.host,
port=settings.run.port,
reload=settings.run.reload,
)