Exception based webdav_check; app startup

This commit is contained in:
Jörn-Michael Miehe 2022-09-18 21:36:52 +00:00
parent 53dd8d74fa
commit bb0115808d
3 changed files with 39 additions and 38 deletions

View file

@ -1,14 +1,11 @@
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",

View file

@ -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,9 +30,12 @@ app = FastAPI(
redoc_url=SETTINGS.redoc_url,
)
app.include_router(v1_router)
app.add_event_handler("startup", webdav_check)
if SETTINGS.production_mode:
@app.on_event("startup")
async def add_middlewares() -> None:
if SETTINGS.production_mode:
# Mount frontend in production mode
app.mount(
path="/",
@ -42,7 +46,7 @@ if SETTINGS.production_mode:
name="frontend",
)
else:
else:
# Allow CORS in debug mode
app.add_middleware(
CORSMiddleware,
@ -54,3 +58,5 @@ else:
allow_headers=["*"],
expose_headers=["*"],
)
app.include_router(v1_router)

View file

@ -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}"