From bb0115808db9683ef317dd730f0583960b3c69b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Sun, 18 Sep 2022 21:36:52 +0000 Subject: [PATCH] Exception based webdav_check; app startup --- api/ovdashboard_api/__main__.py | 15 ++++----- api/ovdashboard_api/app.py | 54 +++++++++++++++++-------------- api/ovdashboard_api/dav_common.py | 8 ++--- 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/api/ovdashboard_api/__main__.py b/api/ovdashboard_api/__main__.py index e983129..0cb503d 100644 --- a/api/ovdashboard_api/__main__.py +++ b/api/ovdashboard_api/__main__.py @@ -1,20 +1,17 @@ from uvicorn import run as uvicorn_run -from .dav_common import webdav_check - def main() -> None: """ If the `main` script is run, `uvicorn` is used to run the app. """ - if webdav_check(): - uvicorn_run( - app="ovdashboard_api:app", - host="0.0.0.0", - port=8000, - reload=True, - ) + uvicorn_run( + app="ovdashboard_api:app", + host="0.0.0.0", + port=8000, + reload=True, + ) if __name__ == "__main__": diff --git a/api/ovdashboard_api/app.py b/api/ovdashboard_api/app.py index d9e0365..56bb583 100644 --- a/api/ovdashboard_api/app.py +++ b/api/ovdashboard_api/app.py @@ -10,6 +10,7 @@ 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 @@ -29,28 +30,33 @@ app = FastAPI( 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) - -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=["*"], - ) diff --git a/api/ovdashboard_api/dav_common.py b/api/ovdashboard_api/dav_common.py index db96631..521faad 100644 --- a/api/ovdashboard_api/dav_common.py +++ b/api/ovdashboard_api/dav_common.py @@ -25,7 +25,7 @@ _WEBDAV_CLIENT = WebDAVclient({ _logger = getLogger(__name__) -def webdav_check() -> bool: +def webdav_check() -> None: """ Checks if base resources are available. """ @@ -53,7 +53,7 @@ def webdav_check() -> bool: "WebDAV connection to %s FAILED!", repr(SETTINGS.webdav.url), ) - return False + raise ConnectionError(SETTINGS.webdav.url) _logger.debug("WebDAV connection ok.") @@ -62,12 +62,10 @@ def webdav_check() -> bool: "WebDAV prefix directory %s NOT FOUND, please create it!", repr(SETTINGS.webdav_prefix), ) - return False + raise FileNotFoundError(SETTINGS.webdav_prefix) _logger.debug("WebDAV prefix directory found.") - return True - def webdav_ensure_path(remote_path: str) -> None: remote_path = f"{SETTINGS.webdav_prefix}/{remote_path}"