-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.py
More file actions
88 lines (67 loc) · 2.51 KB
/
main.py
File metadata and controls
88 lines (67 loc) · 2.51 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
import app_registration
# initialize components BEFORE importing routers that depend on them
app_registration.initialize()
from api.routers import mcp
from api.routers import knowledge as knowledge_router
from api.routers import rag_inference
from api.routers import session as session_router
from api.routers import auth as auth_router
from api.routers import user as user_router
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
async def shutdown_knowledge_module():
"""Shutdown Knowledge module to flush pending BM25 chunks."""
logger.info("Application shutting down...")
try:
knowledge = app_registration.registrator.get_object("knowledge")
if knowledge and hasattr(knowledge, 'shutdown'):
await knowledge.shutdown()
except Exception as e:
logger.error(f"Error during shutdown: {e}")
@asynccontextmanager
async def _noop_context():
"""No-op async context manager for when MCP lifespan is not available."""
yield
@asynccontextmanager
async def lifespan(app: FastAPI):
"""
Lifespan context manager for FastAPI application.
Handles startup and shutdown events for both FastAPI and FastMCP.
"""
# Startup
logger.info("Application starting up...")
# Get MCP lifespan if it exists
mcp_lifespan = getattr(mcp.mcp_app, 'lifespan', None)
# Use MCP lifespan if available, otherwise use a no-op context manager
mcp_context = mcp_lifespan(mcp.mcp_app) if mcp_lifespan else _noop_context()
async with mcp_context:
yield
# Shutdown Knowledge module to flush pending BM25 chunks
await shutdown_knowledge_module()
app = FastAPI(title="RAG-ARC HTTP Server", lifespan=lifespan)
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
@app.get("/")
async def health_check():
logger.info("Health check endpoint called")
return "ok"
app.mount("/mcp", mcp.mcp_app)
app.include_router(knowledge_router.router)
app.include_router(rag_inference.router)
app.include_router(session_router.router)
app.include_router(auth_router.router)
app.include_router(user_router.router)