Files
smart-org/apps/api/app/main.py
T
mjalmoudy94 f1e5df29ce feat: realtime WebSocket layer for live events and streaming logs
- Backend: ConnectionManager, Redis-backed bus with in-process fallback,
  channel authorization, and typed emit helpers wired into workers
  (ai_runtime, sandbox_gateway), workforce/sandbox/billing/audit/error services.
- Frontend: single shared WS client with backoff + heartbeat, RealtimeProvider,
  useLiveChannel/useLiveEvent hooks, live indicators on task detail, manager
  chat, dashboard, traces, sandbox terminal, admin errors, and billing.
- Contracts: WsEvent/WsServerFrame/WsClientFrame types mirroring the port.

All 255 backend tests pass; web typecheck and build pass.
2026-10-13 12:56:36 +03:30

157 lines
5.5 KiB
Python

"""FastAPI application factory.
Nothing in the inner layers imports FastAPI; this module wires them together.
"""
from __future__ import annotations
import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.core.config import settings
from app.core.database import Base, engine
from app.interfaces.http.errors import register_exception_handlers
from app.interfaces.http.request_context import RequestContextMiddleware
from app.interfaces.http.routers import (
admin,
agy,
auth,
billing,
health,
knowledge,
organizations,
projects,
sandbox,
workforce,
)
from app.interfaces.realtime.bus import install as install_realtime_bus
from app.interfaces.realtime.bus import start_relay, stop_relay
from app.interfaces.realtime.router import realtime_manager
from app.interfaces.realtime.router import router as realtime_router
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)-7s %(name)s: %(message)s",
)
logger = logging.getLogger("agy.api")
@asynccontextmanager
async def lifespan(_: FastAPI):
try:
# In development we create any missing tables so `pnpm dev` works on a
# fresh clone. Production schema changes go through Alembic
# (`pnpm db:migrate`).
if not settings.is_production:
import app.infrastructure.db.models # noqa: F401 - register metadata
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
logger.info("Development schema ensured.")
logger.info(
"Adapters -> ai=%s git=%s domain=%s sandbox=%s payment=%s",
settings.ai_default_provider,
settings.git_provider,
settings.domain_router,
settings.sandbox_provider,
settings.payment_provider,
)
except Exception as exc:
# Startup failures are the hardest to diagnose after the fact: the
# process is gone and all that remains is whatever reached stderr, which
# a container runtime routinely discards. Record it, then still fail
# loudly — a half-started API must not accept traffic.
await _record_startup_failure(exc)
raise
# --- realtime wiring ---------------------------------------------------
# This process is the publisher for API-driven events and the relay for
# anything the standalone workers publish. Both are best-effort: if Redis
# is not reachable the in-process fan-out keeps every HTTP-driven feed live.
bus = install_realtime_bus()
bus.attach(realtime_manager().dispatch)
await start_relay()
yield
await stop_relay()
async def _record_startup_failure(exc: BaseException) -> None:
"""Persist a boot failure. Never raises; the caller re-raises regardless."""
try:
from app.domain.audit.models import ErrorSeverity, ErrorSource
from app.infrastructure.error_log import record_error
await record_error(
kind="startup_failed",
message=str(exc) or type(exc).__name__,
exc=exc,
source=ErrorSource.STARTUP,
severity=ErrorSeverity.CRITICAL,
metadata={"app_env": settings.app_env},
)
except Exception: # pragma: no cover - boot is already failing
logger.exception("Could not record the startup failure.")
def create_app() -> FastAPI:
application = FastAPI(
title=settings.app_name,
version="0.1.0",
description="AI Workforce Platform — standalone multi-tenant SaaS.",
lifespan=lifespan,
docs_url="/docs",
openapi_url="/openapi.json",
)
# Starlette applies middleware outermost-first in reverse registration
# order, so the last one added wraps everything else. The request-context
# middleware goes last on purpose: that way even a CORS rejection gets an
# ``X-Request-ID`` — and a CORS rejection is exactly the kind of opaque
# failure where a correlatable id is worth having.
application.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origin_list,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
# Without this the browser cannot read the header off a cross-origin
# response, and the id would be useless to the web client.
expose_headers=["X-Request-ID"],
)
application.add_middleware(RequestContextMiddleware)
register_exception_handlers(application)
application.include_router(health.router)
application.include_router(auth.router, prefix="/api")
application.include_router(organizations.router, prefix="/api")
application.include_router(projects.router, prefix="/api")
application.include_router(workforce.router, prefix="/api")
application.include_router(billing.router, prefix="/api")
application.include_router(knowledge.router, prefix="/api")
application.include_router(sandbox.router, prefix="/api")
application.include_router(admin.router, prefix="/api")
application.include_router(agy.router, prefix="/api")
# The live feed: one WebSocket per browser, every push on it.
application.include_router(realtime_router, prefix="/api")
@application.get("/", tags=["meta"])
async def root() -> dict:
return {
"name": settings.app_name,
"version": "0.1.0",
"docs": "/docs",
"health": "/health",
}
return application
app = create_app()