from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from app.api import auth_routes from app.core.rag import rag_engine from app.api import claims @asynccontextmanager async def lifespan(app: FastAPI): """ Handles startup and shutdown events asynchronously. This guarantees our DB and AI memory are ready before the first request arrives. """ # --- Startup --- rag_engine.initialize() yield # --- Shutdown --- # Initialize the FastAPI application app = FastAPI( title="Plum OPD Adjudication Engine", description="AI-powered automation tool for processing OPD medical claims.", version="1.0.0", lifespan=lifespan ) # Configure Cross-Origin Resource Sharing (CORS) # This allows your frontend (e.g., React on port 3000) to talk to this API app.add_middleware( CORSMiddleware, allow_origins=["*"], # In production, replace "*" with your Vercel frontend URL allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Register our API routes app.include_router(claims.router, prefix="/api") app.include_router(auth_routes.router, prefix="/api") @app.get("/") async def health_check(): """Simple endpoint to verify the server is running.""" return { "status": "online", "service": "Plum OPD Adjudication Engine", "database": "connected", "rag_memory": "loaded" if rag_engine.vector_store else "offline" }