2025-09-09 14:37:32 +05:30
|
|
|
"""Set up logging before importing anything else"""
|
|
|
|
|
|
|
|
|
|
import sentry_sdk
|
|
|
|
|
|
2026-05-27 15:36:48 +05:30
|
|
|
from api.constants import (
|
|
|
|
|
CORS_ALLOWED_ORIGINS,
|
|
|
|
|
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
|
2025-10-09 17:54:31 +05:30
|
|
|
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
|
|
|
|
|
|
2026-04-04 14:26:47 +05:30
|
|
|
from api.constants import REDIS_URL
|
2026-04-21 07:56:16 +05:30
|
|
|
from api.mcp_server import mcp
|
2025-09-09 14:37:32 +05:30
|
|
|
from api.routes.main import router as main_router
|
2026-04-04 14:26:47 +05:30
|
|
|
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"
|
|
|
|
|
|
2026-04-16 13:03:29 +05:30
|
|
|
mcp_app = mcp.http_app(path="/", stateless_http=True)
|
|
|
|
|
|
2025-09-09 14:37:32 +05:30
|
|
|
|
|
|
|
|
@asynccontextmanager
|
|
|
|
|
async def lifespan(app: FastAPI):
|
2026-04-16 13:03:29 +05:30
|
|
|
async with mcp_app.lifespan(app):
|
|
|
|
|
# warmup arq pool
|
|
|
|
|
await get_arq_redis()
|
2025-09-09 14:37:32 +05:30
|
|
|
|
2026-04-16 13:03:29 +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
|
|
|
|
2026-04-16 13:03:29 +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)
|
2026-04-04 14:26:47 +05:30
|
|
|
|
2026-04-16 13:03:29 +05:30
|
|
|
yield # Run app
|
2025-09-09 14:37:32 +05:30
|
|
|
|
2026-04-16 13:03:29 +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,
|
2026-03-14 16:30:02 +05:30
|
|
|
servers=[
|
|
|
|
|
{"url": "https://app.dograh.com", "description": "Production"},
|
|
|
|
|
{"url": "http://localhost:8000", "description": "Local development"},
|
|
|
|
|
],
|
2025-09-09 14:37:32 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-05-27 15:36:48 +05:30
|
|
|
# Configure CORS.
|
|
|
|
|
# OSS is typically deployed with UI and API behind a single reverse proxy
|
|
|
|
|
# (same-origin, so CORS does not apply). Keep it permissive without
|
|
|
|
|
# credentials — wildcard + credentials is rejected by browsers and unsafe.
|
|
|
|
|
# SaaS deployments must set CORS_ALLOWED_ORIGINS to an explicit allowlist.
|
|
|
|
|
if DEPLOYMENT_MODE == "oss":
|
|
|
|
|
cors_origins: list[str] = ["*"]
|
|
|
|
|
cors_allow_credentials = False
|
|
|
|
|
else:
|
|
|
|
|
if not CORS_ALLOWED_ORIGINS:
|
|
|
|
|
raise RuntimeError(
|
|
|
|
|
"CORS_ALLOWED_ORIGINS must be set to an explicit origin allowlist "
|
|
|
|
|
"when DEPLOYMENT_MODE != 'oss'"
|
|
|
|
|
)
|
|
|
|
|
if "*" in CORS_ALLOWED_ORIGINS:
|
|
|
|
|
raise RuntimeError(
|
|
|
|
|
"CORS_ALLOWED_ORIGINS cannot contain '*' with credentialed requests"
|
|
|
|
|
)
|
|
|
|
|
cors_origins = CORS_ALLOWED_ORIGINS
|
|
|
|
|
cors_allow_credentials = True
|
|
|
|
|
|
2025-09-09 14:37:32 +05:30
|
|
|
app.add_middleware(
|
|
|
|
|
CORSMiddleware,
|
2026-05-27 15:36:48 +05:30
|
|
|
allow_origins=cors_origins,
|
|
|
|
|
allow_credentials=cors_allow_credentials,
|
|
|
|
|
allow_methods=["*"],
|
|
|
|
|
allow_headers=["*"],
|
2025-09-09 14:37:32 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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)
|
2026-04-16 13:03:29 +05:30
|
|
|
|
|
|
|
|
# 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)
|