ovdashboard/api/ovdashboard_api/main.py

77 lines
1.6 KiB
Python
Raw Normal View History

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.
"""
from fastapi import FastAPI
2022-09-09 14:19:28 +00:00
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
2022-09-07 00:29:32 +00:00
from uvicorn import run as uvicorn_run
2022-08-28 23:59:57 +00:00
2022-09-07 00:23:31 +00:00
from .dav_common import webdav_check
2022-08-28 23:59:57 +00:00
from .routers import main_router
from .settings import SETTINGS
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",
"email": "jmm@yavook.de",
2022-08-28 23:59:57 +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
)
app.include_router(main_router)
2022-09-15 22:41:23 +00:00
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=["*"],
)
2022-08-28 23:59:57 +00:00
2022-08-28 23:31:17 +00:00
def main() -> None:
2022-09-05 12:54:02 +00:00
"""
If the `main` script is run, `uvicorn` is used to run the app.
"""
2022-09-07 00:23:31 +00:00
if webdav_check():
2022-09-07 00:29:32 +00:00
uvicorn_run(
2022-09-07 00:23:31 +00:00
app="ovdashboard_api.main:app",
2022-09-17 19:00:52 +00:00
host="0.0.0.0",
port=8000,
reload=True,
2022-09-07 00:23:31 +00:00
)
2022-08-28 23:31:17 +00:00
if __name__ == "__main__":
main()