- Fix API routing: redirect_slashes=False + strip trailing slashes from route paths - Fix StaticFiles catch-all intercepting /api/* before FastAPI redirect - Add [tool.hatch.build.targets.wheel] to pyproject.toml for Docker builds - Install docker-compose plugin (v2.34.0) as CLI plugin - Create Dockerfile (python:3.11-alpine, CMD python -m lightsync) - Create docker-compose.prod.yml (Traefik + Authelia, edge network) - Deploy lightsync container to lightsync.groll.cloud - Add LightSync entry to start/projects.json - Update frontend app.js to use /api/devices (no trailing slash)
44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
from contextlib import asynccontextmanager
|
|
from pathlib import Path
|
|
from fastapi import FastAPI
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
from lightsync.devices.registry import DeviceRegistry
|
|
from lightsync.store.show_store import ShowStore
|
|
|
|
# Module-level containers populated in lifespan
|
|
registry: DeviceRegistry | None = None
|
|
show_store: ShowStore | None = None
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
global registry, show_store
|
|
# Startup
|
|
registry = DeviceRegistry(Path("devices.json"))
|
|
await registry.load()
|
|
show_store = ShowStore(Path("shows"))
|
|
show_store.ensure_dir()
|
|
yield
|
|
# Shutdown
|
|
await registry.save()
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
app = FastAPI(title="LightSync", lifespan=lifespan, redirect_slashes=False)
|
|
|
|
# API routes (registered before static mount — see Pattern 7)
|
|
from lightsync.api import shows, devices, ws
|
|
app.include_router(shows.router, prefix="/api/shows")
|
|
app.include_router(devices.router, prefix="/api/devices")
|
|
app.include_router(ws.router)
|
|
|
|
# Static file serving — MUST be last (catches all unmatched paths)
|
|
frontend_dir = Path(__file__).parent / "frontend"
|
|
app.mount("/", StaticFiles(directory=frontend_dir, html=True), name="frontend")
|
|
|
|
return app
|
|
|
|
|
|
app = create_app()
|