use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
#[axum::async_trait]
pub trait Health: Send + Sync + 'static + Clone {
async fn alive(&self) -> HealthResponse;
async fn ready(&self) -> HealthResponse;
}
#[derive(Debug, Clone, Copy)]
pub enum HealthResponse {
OK,
UNAVAILABLE,
}
impl IntoResponse for HealthResponse {
fn into_response(self) -> Response {
match self {
HealthResponse::OK => (StatusCode::OK, "OK").into_response(),
HealthResponse::UNAVAILABLE => {
(StatusCode::SERVICE_UNAVAILABLE, "UNAVAILABLE").into_response()
}
}
}
}
#[derive(Default, Debug, Clone, Copy)]
pub struct AlwaysReadyAndAlive;
#[axum::async_trait]
impl Health for AlwaysReadyAndAlive {
async fn alive(&self) -> HealthResponse {
HealthResponse::OK
}
async fn ready(&self) -> HealthResponse {
HealthResponse::OK
}
}