#!/usr/bin/env python3 """ Main script for `ovdashboard_api` module. Creates the main `FastAPI` app. """ from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles from .dav_common import webdav_check from .routers.v1 import router as v1_router from .settings import SETTINGS app = FastAPI( title="OVDashboard API", description="This API enables the `OVDashboard` service.", contact={ "name": "Jörn-Michael Miehe", "email": "jmm@yavook.de", }, license_info={ "name": "MIT License", "url": "https://opensource.org/licenses/mit-license.php", }, openapi_url=SETTINGS.openapi_url, docs_url=SETTINGS.docs_url, redoc_url=SETTINGS.redoc_url, ) app.add_event_handler("startup", webdav_check) @app.on_event("startup") async def add_middlewares() -> None: if SETTINGS.production_mode: # Mount frontend in production mode app.mount( path="/", app=StaticFiles( directory="/html", html=True, ), name="frontend", ) else: # Allow CORS in debug mode app.add_middleware( CORSMiddleware, allow_origins=[ "*", ], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], expose_headers=["*"], ) app.include_router(v1_router)