62 lines
1.7 KiB
Python
62 lines
1.7 KiB
Python
from contextlib import asynccontextmanager
|
|
from pathlib import Path
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
from app import models # noqa: F401
|
|
from app.api.routes import pages, status
|
|
from app.api.routes.location import router as location_router
|
|
from app.config import get_settings
|
|
from scripts.location_db_adopt import LocationDatabaseAdoptionError, validate_location_runtime_db
|
|
|
|
|
|
def ensure_location_db_ready() -> None:
|
|
settings = get_settings()
|
|
if settings.location_sqlite_path is None:
|
|
return
|
|
|
|
try:
|
|
validate_location_runtime_db(settings.location_database_url)
|
|
except LocationDatabaseAdoptionError as exc:
|
|
raise RuntimeError(str(exc)) from exc
|
|
|
|
|
|
def ensure_runtime_dirs() -> None:
|
|
settings = get_settings()
|
|
for path in (settings.location_sqlite_path, settings.poo_sqlite_path):
|
|
if path is not None:
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(_: FastAPI):
|
|
ensure_runtime_dirs()
|
|
ensure_location_db_ready()
|
|
yield
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
settings = get_settings()
|
|
app = FastAPI(
|
|
title=settings.app_name,
|
|
debug=settings.app_debug,
|
|
version="0.1.0",
|
|
lifespan=lifespan,
|
|
description=(
|
|
"Python rewrite skeleton for the home automation backend. "
|
|
"This stage provides only the foundation for future module migration."
|
|
),
|
|
)
|
|
|
|
static_dir = Path(__file__).parent / "static"
|
|
app.mount("/static", StaticFiles(directory=static_dir), name="static")
|
|
|
|
app.include_router(status.router)
|
|
app.include_router(pages.router)
|
|
app.include_router(location_router)
|
|
return app
|
|
|
|
|
|
app = create_app()
|