from __future__ import annotations from pathlib import Path from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import FileResponse, JSONResponse from fastapi.staticfiles import StaticFiles from live_prototype import build_live_prototype_payload from settings import get_settings BASE_DIR = Path(__file__).resolve().parent DIST_DIR = BASE_DIR / "frontend" / "dist" def create_app() -> FastAPI: app = FastAPI(title="Robot U Community Prototype") app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], ) @app.get("/health") async def health() -> JSONResponse: return JSONResponse({"status": "ok"}) @app.get("/api/prototype") async def prototype() -> JSONResponse: return JSONResponse(await build_live_prototype_payload(get_settings())) if DIST_DIR.exists(): assets_dir = DIST_DIR / "assets" if assets_dir.exists(): app.mount("/assets", StaticFiles(directory=str(assets_dir)), name="assets") @app.get("/{full_path:path}") async def spa_fallback(full_path: str) -> FileResponse: candidate = DIST_DIR / full_path if candidate.is_file(): return FileResponse(str(candidate)) response = FileResponse(str(DIST_DIR / "index.html")) response.headers["Cache-Control"] = "no-store, no-cache, must-revalidate" response.headers["Pragma"] = "no-cache" response.headers["Expires"] = "0" return response return app app = create_app()