""" DRP Backend — FastAPI Application Main entry point with CORS, routing, and DB initialization. """ from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles from fastapi.responses import FileResponse import os from database import init_db from routes.auth import router as auth_router from routes.journey import router as journey_router from routes.admin import router as admin_router from routes.dashboard import router as dashboard_router from routes.programme import router as programme_router from routes.field_staff import router as field_staff_router app = FastAPI( title="Gujarat Digital Reading Programme API", description="Backend for the DRP prototype — StoryWeaver × Gujarat Government", version="2.0.0", ) # CORS — allow all origins for public demo app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Register routers app.include_router(auth_router) app.include_router(journey_router) app.include_router(admin_router) app.include_router(dashboard_router) app.include_router(programme_router) app.include_router(field_staff_router) @app.on_event("startup") def on_startup(): init_db() @app.get("/api/health") def health_check(): return {"status": "ok"} # Serve static files from the 'static' directory # Mount assets first if os.path.exists("static/assets"): app.mount("/assets", StaticFiles(directory="static/assets"), name="assets") # SPA catch-all for routing @app.get("/{full_path:path}") async def serve_spa(full_path: str): if full_path.startswith("api"): raise HTTPException(status_code=404) index_path = os.path.join("static", "index.html") if os.path.exists(index_path): return FileResponse(index_path) return {"message": "Backend is running, but frontend build was not found."}