"""Portable chamber launcher: original relay app at /chamber, static site at /. One process, one port — clone anywhere, `./chamber up`, get the full lab. The upstream live/server.py stays unmodified; this only mounts it. NOTE: Starlette Mounts do not propagate sub-app startup events, so server.startup() (the model load) is invoked explicitly in the lifespan below. Serving starts only after the model is ready. """ import asyncio import os import sys from contextlib import asynccontextmanager from pathlib import Path ROOT = Path(__file__).resolve().parent sys.path.insert(0, str(ROOT / "live")) import server # noqa: E402 (loads env config at import; model loads in startup) from fastapi import FastAPI # noqa: E402 from fastapi.staticfiles import StaticFiles # noqa: E402 SITE = ROOT / "site" @asynccontextmanager async def lifespan(app): # server.startup() is sync and slow (model load); keep the loop alive await asyncio.get_running_loop().run_in_executor(None, server.startup) yield app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None, lifespan=lifespan) app.mount("/chamber", server.app) if SITE.exists(): app.mount("/", StaticFiles(directory=str(SITE), html=True), name="site") else: app.mount("/", server.app) # no site dir: relay everywhere if __name__ == "__main__": import uvicorn port = int(os.environ.get("CHAMBER_PORT", "8484")) uvicorn.run(app, host=os.environ.get("CHAMBER_HOST", "127.0.0.1"), port=port)