-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (53 loc) · 2.07 KB
/
Copy pathmain.py
File metadata and controls
70 lines (53 loc) · 2.07 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
"""FastAPI application entrypoint for the Tapio backend."""
import logging
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.agents.router import AgentRouter
from app.config import BackendSettings
from app.config.config_models import RAGConfig
from app.factories import RAGOrchestratorFactory
from app.memory.checkpointer import get_checkpointer
from app.memory.graph import build_graph
from app.routes import agents, chat, health
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
"""Build the shared orchestrator and agent router once per process lifetime.
Args:
app: The FastAPI application being started.
Yields:
Control back to FastAPI once startup state is attached to ``app.state``.
"""
app.state.orchestrator = RAGOrchestratorFactory(RAGConfig()).create_orchestrator()
app.state.agent_router = AgentRouter()
async with get_checkpointer() as checkpointer:
app.state.graph = build_graph(checkpointer, app.state.orchestrator, app.state.agent_router)
logger.info("Tapio backend started")
yield
def create_app() -> FastAPI:
"""Construct the FastAPI application with routes and CORS configured.
Returns:
The configured, ready-to-serve FastAPI application.
"""
settings = BackendSettings()
app = FastAPI(title="Tapio backend", lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(health.router)
app.include_router(agents.router)
app.include_router(chat.router)
return app
app = create_app()
def run() -> None:
"""Run the backend with uvicorn, honoring ``BackendSettings`` host/port."""
settings = BackendSettings()
uvicorn.run("app.main:app", host=settings.host, port=settings.port, reload=False)
if __name__ == "__main__":
run()