use axum::http::{StatusCode, header};
use axum::response::{IntoResponse, Response};
use super::super::super::auth::{AppState, ResolvedIdentity};
pub fn ensure_debug_access(state: &AppState, identity: &ResolvedIdentity) -> Option<Response> {
if !state.shared.debug_endpoints_enabled {
return Some(json_response(
StatusCode::NOT_FOUND,
r#"{"error":"not found"}"#.to_string(),
));
}
if !identity.0.is_superuser {
return Some(json_response(
StatusCode::FORBIDDEN,
r#"{"error":"superuser required for /cluster/debug/*"}"#.to_string(),
));
}
None
}
pub fn json_response(status: StatusCode, body: String) -> Response {
(status, [(header::CONTENT_TYPE, "application/json")], body).into_response()
}
pub fn ok_json<T: serde::Serialize>(value: &T) -> Response {
match sonic_rs::to_string(value) {
Ok(body) => json_response(StatusCode::OK, body),
Err(e) => {
tracing::warn!(error = %e, "cluster/debug: snapshot serialization failed");
json_response(
StatusCode::INTERNAL_SERVER_ERROR,
r#"{"error":"snapshot serialization failed"}"#.to_string(),
)
}
}
}
pub fn cluster_disabled() -> Response {
json_response(
StatusCode::SERVICE_UNAVAILABLE,
r#"{"error":"cluster mode not enabled"}"#.to_string(),
)
}