From 47aee73574629deeb7ae3824cca9966eee8fa608 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Wed, 7 Sep 2022 00:23:31 +0000 Subject: [PATCH] perform checks before running the API --- api/ovdashboard_api/dav_common.py | 50 +++++++++++++++++++++++++++++++ api/ovdashboard_api/main.py | 14 +++++---- api/ovdashboard_api/settings.py | 8 +++-- 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/api/ovdashboard_api/dav_common.py b/api/ovdashboard_api/dav_common.py index 473ee19..808882f 100644 --- a/api/ovdashboard_api/dav_common.py +++ b/api/ovdashboard_api/dav_common.py @@ -3,6 +3,8 @@ Definition of WebDAV and CalDAV clients. """ from functools import lru_cache +from logging import getLogger +from time import sleep from typing import Any, Iterator from caldav import DAVClient as CalDAVclient @@ -19,6 +21,54 @@ _WEBDAV_CLIENT = WebDAVclient({ "webdav_password": SETTINGS.webdav.password, }) +_logger = getLogger(__name__) + + +def webdav_check() -> bool: + """ + Checks if base resources are available. + """ + + _logger.info( + "Production mode is %s.", + "enabled" if SETTINGS.production_mode else "disabled", + ) + + if SETTINGS.production_mode: + for _ in range(SETTINGS.webdav_retries): + if _WEBDAV_CLIENT.check(""): + break + + _logger.warning( + "Waiting for WebDAV connection to %s ...", + repr(SETTINGS.webdav.url), + ) + sleep(30) + + _logger.debug("WebDAV connection OK!") + + elif _WEBDAV_CLIENT.check(""): + _logger.debug("WebDAV connection OK!") + + else: + _logger.error( + "WebDAV connection to %s FAILED!", + repr(SETTINGS.webdav.url), + ) + return False + + if _WEBDAV_CLIENT.check(SETTINGS.webdav_prefix): + _logger.debug("WebDAV prefix directory FOUND!") + + else: + _logger.error( + "WebDAV prefix directory %s NOT FOUND, please create it!", + repr(SETTINGS.webdav_prefix), + ) + return False + + return True + @lru_cache(maxsize=SETTINGS.cache_size) def webdav_resource(remote_path: Any) -> WebDAVResource: diff --git a/api/ovdashboard_api/main.py b/api/ovdashboard_api/main.py index be43aa7..67a0c51 100644 --- a/api/ovdashboard_api/main.py +++ b/api/ovdashboard_api/main.py @@ -9,6 +9,7 @@ Creates the main `FastAPI` app. import uvicorn from fastapi import FastAPI +from .dav_common import webdav_check from .routers import main_router from .settings import SETTINGS @@ -36,12 +37,13 @@ def main() -> None: If the `main` script is run, `uvicorn` is used to run the app. """ - uvicorn.run( - app="ovdashboard_api.main:app", - host="0.0.0.0", - port=8000, - reload=not SETTINGS.production_mode, - ) + if webdav_check(): + uvicorn.run( + app="ovdashboard_api.main:app", + host="0.0.0.0", + port=8000, + reload=not SETTINGS.production_mode, + ) if __name__ == "__main__": diff --git a/api/ovdashboard_api/settings.py b/api/ovdashboard_api/settings.py index 1877c14..92e20d8 100644 --- a/api/ovdashboard_api/settings.py +++ b/api/ovdashboard_api/settings.py @@ -49,18 +49,20 @@ class Settings(BaseSettings): production_mode: bool = False log_level: str = "DEBUG" + cache_seconds: int = 30 + cache_size: int = 30 + api_v1_prefix: str = "api/v1" openapi_url: str = "/openapi.json" docs_url: Optional[str] = "/docs" redoc_url: Optional[str] = "/redoc" webdav: DavSettings = DavSettings() + webdav_retries: int = 20 webdav_prefix: str = "/ovdashboard" config_path: str = "config.txt" - caldav: DavSettings = DavSettings() - cache_seconds: int = 30 - cache_size: int = 30 + caldav: DavSettings = DavSettings() class Config: env_nested_delimiter = "__"