from fastapi import FastAPI from fastapi.exceptions import RequestValidationError from fastapi.middleware.cors import CORSMiddleware from sqlalchemy.exc import IntegrityError from app.api import auth, mappings, exceptions, exports, reconciliation, tasks, analysis, qa, vouchers from app.core.config import get_settings from app.core.error_handlers import ( generic_exception_handler, http_exception_handler, integrity_error_handler, s2f_exception_handler, validation_exception_handler, ) from app.core.exceptions import S2FException from app.core.logging import configure_logging settings = get_settings() configure_logging(settings.log_level) app = FastAPI( title=settings.app_name, version=settings.app_version, debug=settings.debug, ) app.add_middleware( CORSMiddleware, allow_origins=settings.allowed_origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # 注册异常处理器 app.add_exception_handler(S2FException, s2f_exception_handler) app.add_exception_handler(RequestValidationError, validation_exception_handler) app.add_exception_handler(IntegrityError, integrity_error_handler) app.add_exception_handler(Exception, generic_exception_handler) # 注册路由 app.include_router(auth.router) app.include_router(mappings.router) app.include_router(exceptions.router) app.include_router(exports.router) app.include_router(reconciliation.router) app.include_router(tasks.router) app.include_router(analysis.router) app.include_router(qa.router) app.include_router(vouchers.router) @app.get("/api/health", tags=["health"]) async def health_check() -> dict[str, str]: return { "status": "ok", "service": settings.app_name, "version": settings.app_version, }