# pylint: disable=C0415 """ ClipboardHealthAI application package. This module initializes the FastAPI application, registers all routes, sets up middleware, error handling, and background tasks. """ import asyncio from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from starlette.exceptions import HTTPException as StarletteHTTPException from app.core.wrappers import CbhResponseWrapper, ErrorCbhResponse def create_app() -> FastAPI: """ Create and configure the FastAPI application. """ app = FastAPI() from app.api.scraper import scraper_router app.include_router(scraper_router, tags=["scraper"]) app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], ) @app.exception_handler(StarletteHTTPException) async def http_exception_handler(_, exc): """ Handle HTTP exceptions and convert them to standardized error responses. Args: _: Request object (unused) exc: Exception that was raised Returns: Response: Standardized error response with appropriate status code """ return CbhResponseWrapper( data=None, successful=False, error=ErrorCbhResponse(message=str(exc.detail)) ).response(exc.status_code) @app.on_event("startup") async def startup_event(): """ Execute startup tasks when the application starts. This function creates background tasks for: - Updating Snowflake database tokens - Scheduling statistics collection """ from app.api.scraper.services import schedule_update asyncio.create_task(schedule_update()) @app.get("/") async def read_root(): """ Root endpoint handler that returns a simple greeting message. Returns: dict: A simple greeting message """ return {"message": "Hello world!"} return app