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,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__":

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,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=["*"],
)

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