#!/usr/bin/env python3 """ Main script for `ovdashboard_api` module. Creates the main `FastAPI` app. """ import logging import time from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles from .core.settings import SETTINGS from .core.webdav import WebDAV from .routers import v1_router _logger = logging.getLogger(__name__) 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.include_router(v1_router) _logger.info( "Production mode is %s.", "enabled" if SETTINGS.production_mode else "disabled", ) if SETTINGS.production_mode: # Mount frontend in production mode app.mount( path="/", app=StaticFiles( directory=SETTINGS.ui_directory, html=True, ), name="frontend", ) def check_webdav(retry: int) -> bool | None: if WebDAV._webdav_client.check(""): return True _logger.warning( "WebDAV connection to %s failed (try %d of %d)", repr(SETTINGS.webdav.url), retry + 1, SETTINGS.webdav.retries, ) if retry < SETTINGS.webdav.retries: _logger.debug("Retrying in %d seconds ...", SETTINGS.webdav.retry_delay) time.sleep(SETTINGS.webdav.retry_delay) if not any(check_webdav(n) for n in range(SETTINGS.webdav.retries)): raise ConnectionError("WebDAV connection failed") else: assert WebDAV._webdav_client.check("") # Allow CORS in debug mode app.add_middleware( CORSMiddleware, allow_credentials=True, allow_headers=["*"], allow_methods=["*"], allow_origins=["*"], expose_headers=["*"], ) _logger.debug("WebDAV connection ok.")