# -*- coding: utf-8 -*- """ 健康检查路由 提供服务状态检查端点 """ from fastapi import APIRouter, Request from api.schemas.response import HealthResponse router = APIRouter(prefix="/health", tags=["健康检查"]) # API 版本 API_VERSION = "1.0.0" @router.get( "", response_model=HealthResponse, summary="健康检查", description="检查服务是否正常运行", ) async def health_check(request: Request) -> HealthResponse: """ 基础健康检查 返回服务状态和模型加载状态 """ model_loaded = getattr(request.app.state, "model_loaded", False) return HealthResponse( status="healthy" if model_loaded else "unhealthy", model_loaded=model_loaded, version=API_VERSION, ) @router.get( "/ready", response_model=HealthResponse, summary="就绪检查", description="检查服务是否已准备好处理请求 (模型已加载)", ) async def readiness_check(request: Request) -> HealthResponse: """ 就绪检查 只有当模型加载完成后才返回 healthy """ model_loaded = getattr(request.app.state, "model_loaded", False) return HealthResponse( status="healthy" if model_loaded else "unhealthy", model_loaded=model_loaded, version=API_VERSION, )