"""启动自检:Redis + 关键路由是否挂载(前端 Topbar 条用)。""" from __future__ import annotations from fastapi import APIRouter, FastAPI, Request from app.config.settings import settings router = APIRouter(tags=["platform"]) def build_ready_payload(app: FastAPI, trace_id: str) -> dict: checks: dict[str, bool | str] = {} try: from app.service.risk import redis_gateway gw = redis_gateway.get_gateway() gw.ping() checks["redis"] = True except Exception as exc: # noqa: BLE001 checks["redis"] = False checks["redis_error"] = str(exc)[:120] paths = set(app.openapi().get("paths", {}).keys()) checks["chat_close_all"] = "/api/chat/sessions/close-all" in paths checks["chat_sessions"] = "/api/chat/sessions" in paths checks["analyst_chat"] = "/api/analyst/chat" in paths checks["products_nav_history"] = "/api/products/{product_id}/nav/history" in paths critical = [ checks["chat_close_all"], checks["chat_sessions"], checks["analyst_chat"], checks["products_nav_history"], ] routes_ok = all(critical) redis_ok = checks.get("redis") is True ok = routes_ok return { "ok": ok, "degraded": routes_ok and not redis_ok, "env": settings.app_env, "trace_id": trace_id, "checks": checks, } @router.get("/api/ready") def ready(request: Request): trace_id = getattr(request.state, "trace_id", "unknown") return build_ready_payload(request.app, trace_id)