use axum::extract::State;
use axum::http::{StatusCode, header};
use axum::response::{IntoResponse, Response};
use super::super::auth::{AppState, ResolvedIdentity};
pub async fn cluster_status(
_identity: ResolvedIdentity,
State(state): State<AppState>,
) -> Response {
match state.shared.cluster_observer.get() {
Some(observer) => {
let snap = observer.snapshot();
match sonic_rs::to_string(&snap) {
Ok(body) => json_response(StatusCode::OK, body),
Err(e) => {
tracing::warn!(error = %e, "cluster snapshot serialization failed");
json_response(
StatusCode::INTERNAL_SERVER_ERROR,
r#"{"error":"snapshot serialization failed"}"#.to_string(),
)
}
}
}
None => json_response(
StatusCode::SERVICE_UNAVAILABLE,
r#"{"error":"cluster mode not enabled","detail":"this node is running in single-node mode; /v1/cluster/status requires a [cluster] config section"}"#
.to_string(),
),
}
}
fn json_response(status: StatusCode, body: String) -> Response {
(status, [(header::CONTENT_TYPE, "application/json")], body).into_response()
}