from fastapi import FastAPI, Request from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import FileResponse, JSONResponse from fastapi import HTTPException from . import database as db, config from .logging_config import setup_logging, get_logger from .errors import PipelineError from .routers import settings as settings_router from .routers import sessions as sessions_router from .routers import speakers as speakers_router from .routers import notes as notes_router from .routers import jobs as jobs_router from .routers import models as models_router from .routers import diagnostics as diagnostics_router setup_logging() log = get_logger(__name__) app = FastAPI(title="Nat20 Notes") app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], ) @app.exception_handler(PipelineError) async def pipeline_error_handler(request: Request, exc: PipelineError): log.error("Pipeline error in %s: %s", exc.stage, exc.message, exc_info=exc.cause or exc) return JSONResponse(status_code=502, content={"stage": exc.stage, "message": exc.message}) @app.exception_handler(Exception) async def unhandled_error_handler(request: Request, exc: Exception): log.exception("Unhandled error on %s %s", request.method, request.url.path) return JSONResponse(status_code=500, content={"stage": "unknown", "message": f"Unexpected error: {exc}"}) @app.on_event("startup") def on_startup(): db.init_db() log.info("Database initialized at %s", config.DB_PATH) app.include_router(settings_router.router) app.include_router(sessions_router.router) app.include_router(speakers_router.router) app.include_router(notes_router.router) app.include_router(jobs_router.router) app.include_router(models_router.router) app.include_router(diagnostics_router.router) @app.get("/api/sessions/{session_id}/audio") def get_audio(session_id: str): audio_path = config.AUDIO_DIR / f"{session_id}.wav" if not audio_path.exists(): raise HTTPException(404, "Audio not available for this session") return FileResponse(audio_path, media_type="audio/wav") @app.get("/api/health") def health(): return {"ok": True}