use crate::{HealthCheck, HealthStatus, health::HealthRegistry};
use http::{Response, StatusCode, header};
use http_body_util::Full;
use hyper::body::Bytes;
use serde::Serialize;
use std::sync::Arc;
#[derive(Debug, Clone, Serialize)]
pub struct LiveResponse {
pub overall: HealthStatus,
}
#[derive(Debug, Clone, Serialize)]
pub struct ReadyResponse {
pub overall: HealthStatus,
pub checks: Vec<HealthCheck>,
}
pub async fn live_handler(registry: Arc<HealthRegistry>) -> Response<Full<Bytes>> {
let report = registry.check_liveness().await;
let response = LiveResponse { overall: report.overall };
let body = serde_json::to_vec(&response).unwrap_or_default();
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "application/json")
.body(Full::new(Bytes::from(body)))
.unwrap()
}
pub async fn ready_handler(registry: Arc<HealthRegistry>) -> Response<Full<Bytes>> {
let report = registry.check_readiness().await;
let response = ReadyResponse { overall: report.overall, checks: report.checks };
let body = serde_json::to_vec(&response).unwrap_or_default();
let status =
if matches!(report.overall, HealthStatus::Critical) { StatusCode::SERVICE_UNAVAILABLE } else { StatusCode::OK };
Response::builder()
.status(status)
.header(header::CONTENT_TYPE, "application/json")
.body(Full::new(Bytes::from(body)))
.unwrap()
}