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