perform checks before running the API

This commit is contained in:
Jörn-Michael Miehe 2022-09-07 00:23:31 +00:00
parent c71049e930
commit 47aee73574
3 changed files with 63 additions and 9 deletions

View file

@ -3,6 +3,8 @@ Definition of WebDAV and CalDAV clients.
""" """
from functools import lru_cache from functools import lru_cache
from logging import getLogger
from time import sleep
from typing import Any, Iterator from typing import Any, Iterator
from caldav import DAVClient as CalDAVclient from caldav import DAVClient as CalDAVclient
@ -19,6 +21,54 @@ _WEBDAV_CLIENT = WebDAVclient({
"webdav_password": SETTINGS.webdav.password, "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) @lru_cache(maxsize=SETTINGS.cache_size)
def webdav_resource(remote_path: Any) -> WebDAVResource: def webdav_resource(remote_path: Any) -> WebDAVResource:

View file

@ -9,6 +9,7 @@ Creates the main `FastAPI` app.
import uvicorn import uvicorn
from fastapi import FastAPI from fastapi import FastAPI
from .dav_common import webdav_check
from .routers import main_router from .routers import main_router
from .settings import SETTINGS from .settings import SETTINGS
@ -36,6 +37,7 @@ def main() -> None:
If the `main` script is run, `uvicorn` is used to run the app. If the `main` script is run, `uvicorn` is used to run the app.
""" """
if webdav_check():
uvicorn.run( uvicorn.run(
app="ovdashboard_api.main:app", app="ovdashboard_api.main:app",
host="0.0.0.0", host="0.0.0.0",

View file

@ -49,18 +49,20 @@ class Settings(BaseSettings):
production_mode: bool = False production_mode: bool = False
log_level: str = "DEBUG" log_level: str = "DEBUG"
cache_seconds: int = 30
cache_size: int = 30
api_v1_prefix: str = "api/v1" api_v1_prefix: str = "api/v1"
openapi_url: str = "/openapi.json" openapi_url: str = "/openapi.json"
docs_url: Optional[str] = "/docs" docs_url: Optional[str] = "/docs"
redoc_url: Optional[str] = "/redoc" redoc_url: Optional[str] = "/redoc"
webdav: DavSettings = DavSettings() webdav: DavSettings = DavSettings()
webdav_retries: int = 20
webdav_prefix: str = "/ovdashboard" webdav_prefix: str = "/ovdashboard"
config_path: str = "config.txt" config_path: str = "config.txt"
caldav: DavSettings = DavSettings()
cache_seconds: int = 30 caldav: DavSettings = DavSettings()
cache_size: int = 30
class Config: class Config:
env_nested_delimiter = "__" env_nested_delimiter = "__"