| """FastAPI application initialization""" |
|
|
| from fastapi import FastAPI |
| from fastapi.middleware.cors import CORSMiddleware |
| from contextlib import asynccontextmanager |
| import logging |
| from .core.config import get_settings |
| from .api.routes import health, documents, rag, config as config_routes |
|
|
| |
| logging.basicConfig( |
| level=logging.INFO, |
| format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", |
| ) |
| logger = logging.getLogger(__name__) |
|
|
|
|
| @asynccontextmanager |
| async def lifespan(app: FastAPI): |
| """Application lifespan context manager""" |
| logger.info("RAG Application starting...") |
| yield |
| logger.info("RAG Application shutting down...") |
|
|
|
|
| def create_app() -> FastAPI: |
| """Create and configure FastAPI application""" |
| settings = get_settings() |
|
|
| app = FastAPI( |
| title="RAG Application", |
| description="Retrieval-Augmented Generation with Groq and Multiple Vector Databases", |
| version="1.0.0", |
| lifespan=lifespan, |
| ) |
|
|
| |
| app.add_middleware( |
| CORSMiddleware, |
| allow_origins=[settings.frontend_url, "http://localhost:3000", "http://localhost:8000"], |
| allow_credentials=True, |
| allow_methods=["*"], |
| allow_headers=["*"], |
| ) |
|
|
| |
| app.include_router(health.router, prefix="/api/v1", tags=["Health"]) |
| app.include_router(documents.router, prefix="/api/v1", tags=["Documents"]) |
| app.include_router(rag.router, prefix="/api/v1", tags=["RAG"]) |
| app.include_router(config_routes.router, prefix="/api/v1", tags=["Configuration"]) |
|
|
| logger.info("RAG Application created successfully") |
| return app |
|
|
|
|
| app = create_app() |
|
|
|
|
| if __name__ == "__main__": |
| import uvicorn |
|
|
| settings = get_settings() |
| uvicorn.run( |
| "app.main:app", |
| host="0.0.0.0", |
| port=8000, |
| reload=settings.debug, |
| log_level=settings.log_level.lower(), |
| ) |
|
|