#![cfg(feature = "server-health-check")]
use axum::extract::State;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::Json;
use serde_json::json;
#[derive(Debug, Clone, Copy)]
pub struct HealthDetailsEnabled(pub bool);
pub async fn liveness_handler() -> Response {
Json(json!({ "status": "healthy" })).into_response()
}
pub async fn readiness_handler(State(enabled): State<HealthDetailsEnabled>) -> Response {
let upstream = sdforge::health::readyz_handler().await;
let status = upstream.status();
let bytes = match axum::body::to_bytes(upstream.into_body(), 256 * 1024).await {
Ok(b) => b,
Err(e) => {
tracing::warn!(error = %e, "{}", crate::i18n::translate_detail("logs-server-readiness-body-read-failed", &[]));
return (
StatusCode::SERVICE_UNAVAILABLE,
Json(json!({ "status": "unknown" })),
)
.into_response();
},
};
let mut value: serde_json::Value = match serde_json::from_slice(&bytes) {
Ok(v) => v,
Err(e) => {
tracing::warn!(error = %e, "{}", crate::i18n::translate_detail("logs-server-readiness-body-not-json", &[]));
return (
StatusCode::SERVICE_UNAVAILABLE,
Json(json!({ "status": "unknown" })),
)
.into_response();
},
};
if !enabled.0 {
if let Some(checks) = value.get_mut("checks").and_then(|c| c.as_array_mut()) {
for check in checks {
if let Some(obj) = check.as_object_mut() {
obj.remove("details");
}
}
}
}
(status, Json(value)).into_response()
}