from fastapi import FastAPI, Request from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse from config import settings from core.exceptions import FileValidationError, InsufficientCreditsError, ConversionError, ExportParseError from core.logging import setup_logging from routers import upload, users, payments setup_logging() app = FastAPI(title='IDconvert API', version='0.1.0') app.add_middleware( CORSMiddleware, allow_origins=['http://localhost:3000', 'http://localhost:3001', 'http://127.0.0.1:3000', 'http://127.0.0.1:3001'], allow_credentials=True, allow_methods=['*'], allow_headers=['*'], ) # ── Exception handlers ──────────────────────────────────────────────────────── @app.exception_handler(FileValidationError) async def file_validation_handler(request: Request, exc: FileValidationError): return JSONResponse( status_code=422, content={'error': exc.error_code, 'message': exc.message}, ) @app.exception_handler(InsufficientCreditsError) async def credits_handler(request: Request, exc: InsufficientCreditsError): return JSONResponse( status_code=402, content={'error': 'INSUFFICIENT_CREDITS', 'message': str(exc)}, ) @app.exception_handler(ConversionError) async def conversion_handler(request: Request, exc: ConversionError): return JSONResponse( status_code=500, content={'error': 'CONVERSION_FAILED', 'message': str(exc)}, ) @app.exception_handler(ExportParseError) async def parse_error_handler(request: Request, exc: ExportParseError): return JSONResponse( status_code=422, content={'error': 'EXPORT_PARSE_ERROR', 'message': str(exc)}, ) # ── Routers ─────────────────────────────────────────────────────────────────── app.include_router(upload.router) app.include_router(users.router) app.include_router(payments.router) @app.get('/health') async def health(): return {'status': 'ok'}