use axum::{extract::State, routing::get, Json};
use serde::Serialize;
use super::{format, routes::Routes};
use crate::{app::AppContext, redis, Result};
#[derive(Serialize)]
struct Health {
pub ok: bool,
}
async fn health(State(ctx): State<AppContext>) -> Result<Json<Health>> {
let mut is_ok = match ctx.db.ping().await {
Ok(()) => true,
Err(error) => {
tracing::error!("Database ping error: {:?}", error);
false
}
};
if let Some(pool) = ctx.redis {
if let Err(error) = redis::ping(&pool).await {
tracing::error!("Redis ping error: {:?}", error);
is_ok = false;
}
}
format::json(Health { ok: is_ok })
}
pub fn routes() -> Routes {
Routes::new().add("/_health", get(health))
}