dograh/api/app.py

108 lines
3.1 KiB
Python
Raw Normal View History

2025-09-09 14:37:32 +05:30
"""Set up logging before importing anything else"""
import sentry_sdk
from api.constants import DEPLOYMENT_MODE, ENABLE_TELEMETRY, SENTRY_DSN
2025-09-09 14:37:32 +05:30
from api.logging_config import ENVIRONMENT, setup_logging
# Set up logging and get the listener for cleanup
setup_logging()
if SENTRY_DSN and (
DEPLOYMENT_MODE != "oss" or (DEPLOYMENT_MODE == "oss" and ENABLE_TELEMETRY)
):
sentry_sdk.init(
dsn=SENTRY_DSN,
send_default_pii=True,
environment=ENVIRONMENT,
)
print(f"Sentry initialized in environment: {ENVIRONMENT}")
2025-09-09 14:37:32 +05:30
from contextlib import asynccontextmanager
from fastapi import APIRouter, FastAPI
from fastapi.middleware.cors import CORSMiddleware
from loguru import logger
from api.constants import REDIS_URL
from api.mcp import mcp
2025-09-09 14:37:32 +05:30
from api.routes.main import router as main_router
from api.services.pipecat.tracing_config import (
handle_langfuse_sync,
load_all_org_langfuse_credentials,
)
from api.services.worker_sync.manager import (
WorkerSyncManager,
set_worker_sync_manager,
)
from api.services.worker_sync.protocol import WorkerSyncEventType
2025-09-09 14:37:32 +05:30
from api.tasks.arq import get_arq_redis
API_PREFIX = "/api/v1"
mcp_app = mcp.http_app(path="/", stateless_http=True)
2025-09-09 14:37:32 +05:30
@asynccontextmanager
async def lifespan(app: FastAPI):
async with mcp_app.lifespan(app):
# warmup arq pool
await get_arq_redis()
2025-09-09 14:37:32 +05:30
# Pre-register all org-specific Langfuse exporters so they're ready
# before any pipeline runs, without per-call DB lookups.
await load_all_org_langfuse_credentials()
2026-03-23 11:36:39 +05:30
# Start cross-worker sync manager so config changes propagate to all workers
sync_manager = WorkerSyncManager(REDIS_URL)
sync_manager.register(
WorkerSyncEventType.LANGFUSE_CREDENTIALS, handle_langfuse_sync
)
await sync_manager.start()
set_worker_sync_manager(sync_manager)
yield # Run app
2025-09-09 14:37:32 +05:30
# Shutdown sequence - this runs when FastAPI is shutting down
logger.info("Starting graceful shutdown...")
await sync_manager.stop()
2025-09-09 14:37:32 +05:30
app = FastAPI(
title="Dograh API",
description="API for the Dograh app",
version="1.0.0",
openapi_url=f"{API_PREFIX}/openapi.json",
lifespan=lifespan,
servers=[
{"url": "https://app.dograh.com", "description": "Production"},
{"url": "http://localhost:8000", "description": "Local development"},
],
2025-09-09 14:37:32 +05:30
)
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
api_router = APIRouter()
# include subrouters here
api_router.include_router(main_router)
# main router with api prefix
app.include_router(api_router, prefix=API_PREFIX)
# Mount the MCP server — agents reach it at /api/v1/mcp over Streamable HTTP,
# authenticating with the same X-API-Key header used by the REST API.
# Mounted under /api/v1 so existing reverse-proxy rules (nginx etc.) route it
# without any extra configuration.
app.mount(f"{API_PREFIX}/mcp", mcp_app)