""" api/main.py — FastAPI application with lifespan, CORS, and health check """ import os import tempfile from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from loguru import logger from api.routes import ingest, draft, feedback, patterns, evidence @asynccontextmanager async def lifespan(app: FastAPI): """Startup and shutdown events.""" logger.info("Legal Document Intelligence System starting...") # Import dependencies to trigger initialisation from api import dependencies # noqa: F401 logger.info("All components initialised.") yield logger.info("Shutting down.") app = FastAPI( title="Legal Document Intelligence System", description=( "Production-grade AI pipeline for legal document ingestion, " "retrieval, grounded draft generation, and continuous improvement." ), version="1.0.0", lifespan=lifespan, ) # CORS app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Routes app.include_router(ingest.router, tags=["Ingestion"]) app.include_router(draft.router, tags=["Draft Generation"]) app.include_router(feedback.router, tags=["Feedback Loop"]) app.include_router(patterns.router, tags=["Patterns"]) app.include_router(evidence.router, tags=["Evidence"]) @app.get("/health") async def health(): from api.dependencies import vectorstore, bm25_index, pattern_store return { "status": "healthy", "components": { "vector_store_chunks": vectorstore.count(), "bm25_chunks": bm25_index.count(), "active_patterns": pattern_store.pattern_count(), }, }