use super::{HealthRegistry, HealthStatus};
use std::sync::Arc;
use warp::http::StatusCode;
use warp::reply::json;
use warp::{Filter, Reply};
pub fn live_filter() -> impl Filter<Extract = (impl Reply,), Error = warp::Rejection> + Clone {
warp::path!("health" / "live").and(warp::get()).map(|| {
warp::reply::with_status(
json(&serde_json::json!({"status": "healthy"})),
StatusCode::OK,
)
})
}
pub fn ready_filter(
registry: Arc<HealthRegistry>,
) -> impl Filter<Extract = (impl Reply,), Error = warp::Rejection> + Clone {
let registry = warp::any().map(move || registry.clone());
warp::path!("health" / "ready")
.and(warp::get())
.and(registry)
.and_then(|registry: Arc<HealthRegistry>| async move {
let report = registry.check_all().await;
let status = match report.overall {
HealthStatus::Healthy | HealthStatus::Degraded => StatusCode::OK,
HealthStatus::Unhealthy => StatusCode::SERVICE_UNAVAILABLE,
};
Ok::<_, std::convert::Infallible>(warp::reply::with_status(json(&report), status))
})
}
pub fn health_filters(
registry: Arc<HealthRegistry>,
) -> impl Filter<Extract = (impl Reply,), Error = warp::Rejection> + Clone {
live_filter().or(ready_filter(registry))
}