perform checks before running the API
This commit is contained in:
parent
c71049e930
commit
47aee73574
3 changed files with 63 additions and 9 deletions
|
@ -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:
|
||||||
|
|
|
@ -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,12 +37,13 @@ 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.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
uvicorn.run(
|
if webdav_check():
|
||||||
app="ovdashboard_api.main:app",
|
uvicorn.run(
|
||||||
host="0.0.0.0",
|
app="ovdashboard_api.main:app",
|
||||||
port=8000,
|
host="0.0.0.0",
|
||||||
reload=not SETTINGS.production_mode,
|
port=8000,
|
||||||
)
|
reload=not SETTINGS.production_mode,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -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 = "__"
|
||||||
|
|
Loading…
Reference in a new issue