kds/backend/main.py
jtricerolph 23889315f5 Scoped DB role + own theme colour (port log E17)
- Split database.py into a runtime engine (scoped `kds` DB role, used for
  all request handling) and a migration engine (privileged `kitchen` role,
  used only at startup to create KDS's own tables and ALTER kitchen_settings)
  — KDS previously shared kitchen's full-access DB credential wholesale
- Dispose both engines on shutdown (main.py)
- Fix theme colour clash: KDS was accidentally seeded with kitchen's teal
  (#0d9488) instead of its own colour — now #ea580c (orange), regenerated
  PWA icons to match

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-06 14:45:01 +00:00

72 lines
2.2 KiB
Python

import logging
import os
import time
from contextlib import asynccontextmanager
from fastapi import FastAPI
from database import engine, runtime_engine, Base
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
from api import kds as kds_api
from migrations.add_kds_tables import run_migration as run_kds_tables
from migrations.add_kds_course_flow import run_migration as run_kds_course_flow
from migrations.add_kds_order_tracking import run_migration as run_kds_order_tracking
from migrations.add_kds_bookings_refresh import run_migration as run_kds_bookings_refresh
from services.signalr_listener import start_signalr_listener, stop_signalr_listener
logger = logging.getLogger(__name__)
async def _run(name, coro):
try:
await coro()
logger.info(f"{name} migration completed")
except Exception as e:
logger.warning(f"{name} migration warning (may be expected): {e}")
@asynccontextmanager
async def lifespan(app: FastAPI):
# Create any SQLAlchemy-mapped KDS tables (idempotent). Uses the
# privileged migration engine (`kitchen` role) — the scoped `kds` role
# used for request handling can't CREATE TABLE.
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
# KDS migrations — extend kitchen_db schema with KDS columns/tables.
# Requires kitchen to be deployed first (kitchen_settings must exist).
await _run("KDS tables", run_kds_tables)
await _run("KDS course flow", run_kds_course_flow)
await _run("KDS order tracking", run_kds_order_tracking)
await _run("KDS bookings refresh", run_kds_bookings_refresh)
await start_signalr_listener()
yield
await stop_signalr_listener()
await engine.dispose()
await runtime_engine.dispose()
app = FastAPI(
title="KDS",
description="Kitchen Display System — SambaPOS SignalR ticket feed, course flow",
version="1.0.0",
lifespan=lifespan,
)
STARTED_AT = str(int(time.time() * 1000))
app.include_router(kds_api.router, prefix="/api/kds", tags=["KDS"])
@app.get("/health")
async def health_check():
return {"status": "healthy", "version": os.environ.get("BUILD_VERSION", STARTED_AT)}