from fastapi import FastAPI, Request from fastapi.exceptions import RequestValidationError from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse from app.api.discovery import router as discovery_router from app.api.evaluations import router as evaluations_router from app.api.health import router as health_router from app.api.offers import router as offers_router from app.api.products import router as products_router from app.api.profiles import router as profiles_router from app.api.projects import router as projects_router from app.errors import AppError, error_body from app.settings import get_settings settings = get_settings() app = FastAPI( title="Buying Agent", version="0.1.0", description="Generic buying and deal evaluation API. The backend is the system of record; no frontend or n8n is required.", ) app.add_middleware( CORSMiddleware, allow_origins=settings.cors_origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) app.include_router(health_router) app.include_router(profiles_router) app.include_router(products_router) app.include_router(offers_router) app.include_router(evaluations_router) app.include_router(projects_router) app.include_router(discovery_router) @app.exception_handler(AppError) def handle_app_error(_request: Request, exc: AppError) -> JSONResponse: return JSONResponse(error_body(exc.code, exc.message, exc.details), status_code=exc.status_code) @app.exception_handler(RequestValidationError) def handle_validation_error(_request: Request, exc: RequestValidationError) -> JSONResponse: return JSONResponse( error_body("VALIDATION_ERROR", "request validation failed", {"errors": exc.errors()}), status_code=422, )