from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.exceptions import RequestValidationError from starlette.exceptions import HTTPException as StarletteHTTPException from app.config.settings import settings from app.database.connection import init_db from app.middleware.error_handler import ( http_exception_handler, validation_exception_handler, general_exception_handler ) from app.routes import auth, chat, sessions, documents, news, admin_policies, reports from app.routes import settings as settings_router # Create FastAPI app app = FastAPI( title="Builder's AI API", description="Construction AI Assistant API with Multi-Agent RAG System", version="1.0.0" ) # CORS middleware app.add_middleware( CORSMiddleware, allow_origins=settings.cors_origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Exception handlers app.add_exception_handler(StarletteHTTPException, http_exception_handler) app.add_exception_handler(RequestValidationError, validation_exception_handler) app.add_exception_handler(Exception, general_exception_handler) # Include routers app.include_router(auth.router) app.include_router(chat.router) app.include_router(sessions.router) app.include_router(documents.router) app.include_router(news.router) app.include_router(admin_policies.router) app.include_router(reports.router) app.include_router(settings_router.router) @app.on_event("startup") async def startup_event(): """Initialize database on startup.""" print("Initializing database...") init_db() print("Database initialized successfully!") @app.on_event("shutdown") async def shutdown_event(): """Cleanup on shutdown.""" print("Shutting down...") @app.get("/") async def root(): """Root endpoint.""" return { "message": "Welcome to Builder's AI API", "version": "1.0.0", "docs": "/docs" } @app.get("/health") async def health_check(): """Health check endpoint.""" return {"status": "healthy"} if __name__ == "__main__": import uvicorn from app.config.settings import settings as app_settings uvicorn.run(app, host="0.0.0.0", port=app_settings.PORT)