2022-08-28 23:59:57 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
"""
|
2022-09-05 12:54:02 +00:00
|
|
|
Main script for `ovdashboard_api` module.
|
2022-08-28 23:59:57 +00:00
|
|
|
|
|
|
|
Creates the main `FastAPI` app.
|
|
|
|
"""
|
|
|
|
|
2023-10-20 08:43:15 +00:00
|
|
|
import logging
|
|
|
|
import time
|
|
|
|
|
2022-08-28 23:59:57 +00:00
|
|
|
from fastapi import FastAPI
|
2022-09-09 14:19:28 +00:00
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from fastapi.staticfiles import StaticFiles
|
2022-08-28 23:59:57 +00:00
|
|
|
|
2023-10-18 16:55:54 +00:00
|
|
|
from .core.settings import SETTINGS
|
2023-10-20 08:43:15 +00:00
|
|
|
from .core.webdav import WebDAV
|
2022-09-19 09:35:18 +00:00
|
|
|
from .routers import v1_router
|
2022-08-28 23:59:57 +00:00
|
|
|
|
2023-10-20 08:43:15 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
2022-08-28 23:59:57 +00:00
|
|
|
app = FastAPI(
|
2022-08-31 18:26:07 +00:00
|
|
|
title="OVDashboard API",
|
|
|
|
description="This API enables the `OVDashboard` service.",
|
2022-08-28 23:59:57 +00:00
|
|
|
contact={
|
|
|
|
"name": "Jörn-Michael Miehe",
|
2022-08-29 00:50:23 +00:00
|
|
|
"email": "jmm@yavook.de",
|
2022-08-28 23:59:57 +00:00
|
|
|
},
|
2022-09-05 21:36:07 +00:00
|
|
|
license_info={
|
|
|
|
"name": "MIT License",
|
|
|
|
"url": "https://opensource.org/licenses/mit-license.php",
|
|
|
|
},
|
2022-08-28 23:59:57 +00:00
|
|
|
openapi_url=SETTINGS.openapi_url,
|
2022-09-07 11:58:43 +00:00
|
|
|
docs_url=SETTINGS.docs_url,
|
|
|
|
redoc_url=SETTINGS.redoc_url,
|
2022-08-28 23:59:57 +00:00
|
|
|
)
|
|
|
|
|
2022-09-18 21:36:52 +00:00
|
|
|
app.include_router(v1_router)
|
2023-10-20 08:43:15 +00:00
|
|
|
|
|
|
|
_logger.info(
|
|
|
|
"Production mode is %s.",
|
|
|
|
"enabled" if SETTINGS.production_mode else "disabled",
|
|
|
|
)
|
2023-10-16 18:06:55 +00:00
|
|
|
|
|
|
|
if SETTINGS.production_mode:
|
|
|
|
# Mount frontend in production mode
|
|
|
|
app.mount(
|
|
|
|
path="/",
|
|
|
|
app=StaticFiles(
|
|
|
|
directory=SETTINGS.ui_directory,
|
|
|
|
html=True,
|
|
|
|
),
|
|
|
|
name="frontend",
|
|
|
|
)
|
|
|
|
|
2023-10-20 08:43:15 +00:00
|
|
|
for _ in range(SETTINGS.webdav.retries):
|
|
|
|
if WebDAV._webdav_client.check(""):
|
|
|
|
break
|
|
|
|
|
|
|
|
_logger.warning(
|
|
|
|
"Waiting for WebDAV connection to %s ...",
|
|
|
|
repr(SETTINGS.webdav.url),
|
|
|
|
)
|
|
|
|
time.sleep(30)
|
|
|
|
|
2023-10-16 18:06:55 +00:00
|
|
|
else:
|
2023-10-20 08:43:15 +00:00
|
|
|
assert WebDAV._webdav_client.check("")
|
|
|
|
|
2023-10-16 18:06:55 +00:00
|
|
|
# Allow CORS in debug mode
|
|
|
|
app.add_middleware(
|
|
|
|
CORSMiddleware,
|
|
|
|
allow_credentials=True,
|
|
|
|
allow_headers=["*"],
|
2023-10-20 08:43:15 +00:00
|
|
|
allow_methods=["*"],
|
|
|
|
allow_origins=["*"],
|
2023-10-16 18:06:55 +00:00
|
|
|
expose_headers=["*"],
|
|
|
|
)
|
2023-10-20 08:43:15 +00:00
|
|
|
|
|
|
|
_logger.debug("WebDAV connection ok.")
|