use axum::Json;
use axum::extract::State;
use axum::http::StatusCode;
use axum::response::IntoResponse;
use crate::state::AppState;
pub(super) async fn healthz() -> impl IntoResponse {
Json(serde_json::json!({"status": "ok"}))
}
pub(super) async fn readyz(State(state): State<AppState>) -> impl IntoResponse {
let snapshot = state.readiness.borrow().clone();
let mut ready = snapshot.is_ready();
let mut checks = serde_json::Map::new();
checks.insert("storage".to_string(), snapshot.storage.to_json());
checks.insert("search".to_string(), snapshot.search.to_json());
if let Some(events) = &state.events {
let connected = events.ready();
ready &= connected;
let check = if connected {
serde_json::json!({ "backend": events.backend_name(), "status": "ok" })
} else {
serde_json::json!({
"backend": events.backend_name(),
"status": "unavailable",
"reason": "disconnected",
})
};
checks.insert("events".to_string(), check);
}
let status = if ready {
StatusCode::OK
} else {
StatusCode::SERVICE_UNAVAILABLE
};
let word = if !ready {
"unavailable"
} else if snapshot.is_degraded() {
"degraded"
} else {
"ok"
};
(
status,
Json(serde_json::json!({
"status": word,
"checks": checks,
})),
)
}