File size: 1,850 Bytes
b106f38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""FastAPI Application Main Entrypoint.

Exposes REST APIs for health checking, document upload/indexing, and RAG queries.
"""

from contextlib import asynccontextmanager
import logging
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from app.api.routes_health import router as health_router
from app.api.routes_query import router as query_router
from app.api.routes_upload import router as upload_router
from app.config import get_settings, setup_logging
from app.state import get_app_state


logger = logging.getLogger(__name__)


@asynccontextmanager
async def lifespan(app: FastAPI):
    """Application lifespan context: initialize logging and warm up services on startup."""
    settings = get_settings()
    setup_logging(settings.log_level)
    logger.info("Initializing PDF Knowledge Assistant API server...")
    # Trigger lazy singleton initialization
    state = get_app_state()
    logger.info("Services ready. Vector store contains %d chunks.", state.vector_store.total_chunks())
    yield
    logger.info("Shutting down PDF Knowledge Assistant API server.")


app = FastAPI(
    title="📚 PDF Knowledge Assistant API",
    description="Production-grade Retrieval-Augmented Generation (RAG) backend for PDF documents.",
    version="1.0.0",
    lifespan=lifespan,
)

# Enable CORS for frontend and development origins
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Register route modules
app.include_router(health_router)
app.include_router(upload_router)
app.include_router(query_router)


if __name__ == "__main__":
    import uvicorn
    settings = get_settings()
    uvicorn.run(
        "app.main:app",
        host=settings.app_host,
        port=settings.app_port,
        reload=False,
    )