use axum::extract::State;
use axum::http::StatusCode;
use axum::http::header;
use axum::response::{IntoResponse, Response};
use serde_json::json;
use crate::store::Store;
pub async fn health(State(store): State<Store>) -> Response {
match store.check_ready() {
Ok(()) => (
StatusCode::OK,
axum::Json(json!({
"status": "UP",
"components": {
"store": { "status": "UP" }
}
})),
)
.into_response(),
Err(err) => (
StatusCode::SERVICE_UNAVAILABLE,
axum::Json(json!({
"status": "DOWN",
"components": {
"store": { "status": "DOWN", "detail": err.to_string() }
}
})),
)
.into_response(),
}
}
pub async fn live() -> (StatusCode, &'static str) {
(StatusCode::OK, "OK")
}
pub async fn ready(State(store): State<Store>) -> (StatusCode, &'static str) {
match store.check_ready() {
Ok(()) => (StatusCode::OK, "OK"),
Err(_) => (StatusCode::SERVICE_UNAVAILABLE, "Service Unavailable"),
}
}
pub async fn metrics(State(store): State<Store>) -> Response {
(
StatusCode::OK,
[(
header::CONTENT_TYPE,
"text/plain; version=0.0.4; charset=utf-8",
)],
store.render_metrics().await,
)
.into_response()
}