garrison 0.9.0

Garrison - 面向 Rust 生态的一站式身份认证鉴权框架
Documentation
// Copyright (c) 2026 Kirky.X🌠
// SPDX-License-Identifier: Apache-2.0

//! actix-web 框架的健康检查路由集成:提供 `/health/live` 与 `/health/ready` 端点。

use super::{HealthRegistry, HealthStatus};
use actix_web::http::StatusCode;
use actix_web::{web, HttpResponse, Responder};
use std::sync::Arc;

/// Liveness 探针 handler。
pub async fn live() -> impl Responder {
    HttpResponse::Ok().json(serde_json::json!({"status": "healthy"}))
}

/// Readiness 探针 handler。
///
/// 单项检查超时护栏(默认 5 秒,[`HealthRegistry::with_check_timeout`] 可配置)由
/// [`HealthRegistry::check_all`] 内部强制执行:挂起/panic 的检查按 `Unhealthy` 聚合,
/// 不会阻塞 actix worker 线程。
pub async fn ready(registry: web::Data<Arc<HealthRegistry>>) -> HttpResponse {
    let report = registry.check_all().await;
    let status = match report.overall {
        HealthStatus::Healthy | HealthStatus::Degraded => StatusCode::OK,
        HealthStatus::Unhealthy => StatusCode::SERVICE_UNAVAILABLE,
    };
    HttpResponse::build(status).json(report)
}

/// 注册健康检查路由到 actix-web App。
///
/// # 示例
///
/// ```ignore
/// use garrison::health::actix_routes::configure_health_routes;
/// use garrison::health::HealthRegistry;
/// use std::sync::Arc;
///
/// let registry = Arc::new(HealthRegistry::new());
/// let app = actix_web::App::new()
/// .app_data(actix_web::web::Data::new(registry))
/// .configure(configure_health_routes);
/// ```
pub fn configure_health_routes(cfg: &mut web::ServiceConfig) {
    cfg.service(web::resource("/health/live").route(web::get().to(live)));
    cfg.service(web::resource("/health/ready").route(web::get().to(ready)));
}