import importlib import os import pkgutil from pathlib import Path from fastapi import FastAPI, Request from fastapi.responses import JSONResponse # NO request-size cap. Payload bytes are streamed straight to disk by the # artifact endpoint; JSON bodies are the only in-memory part (uvicorn default). # Disk is the only size limit — failure surfaces as an error, never a silent truncation. app = FastAPI(title="Archx Relay v4", version="4.0.0") _routers = [] projects_dir = Path(__file__).parent / "projects" if projects_dir.exists(): for module_info in pkgutil.iter_modules([str(projects_dir)]): if not module_info.ispkg: continue name = module_info.name try: mod = importlib.import_module(f"projects.{name}.router") app.include_router(mod.router, prefix=f"/{name}") _routers.append(name) except Exception as e: print(f"Failed to load project '{name}': {e}", flush=True) @app.get("/health") def health(): return {"online": "yes", "projects": _routers} @app.on_event("shutdown") def _shutdown(): for name in _routers: try: mod = importlib.import_module(f"projects.{name}.router") if hasattr(mod, "store"): mod.store.stop() except Exception as e: print(f"shutdown flush failed for {name}: {e}", flush=True)