use axum::http::StatusCode;
use axum::response::IntoResponse;
use sqlx::Database;
use aro_web::dep::Dep;
#[expect(
clippy::unwrap_used,
reason = "serializing a literal serde_json::json! value cannot fail"
)]
pub(crate) async fn health_db_handler<DB: Database>(
Dep(pool): Dep<fletch_orm::Pool<DB>>,
) -> impl IntoResponse {
if pool.inner().acquire().await.is_ok() {
let body = serde_json::json!({
"status": "ok",
"pool_size": pool.size(),
"idle_connections": pool.num_idle(),
});
(
StatusCode::OK,
[(axum::http::header::CONTENT_TYPE, "application/json")],
serde_json::to_string(&body).unwrap(),
)
} else {
let body = serde_json::json!({
"status": "error",
"message": "database unreachable",
});
(
StatusCode::SERVICE_UNAVAILABLE,
[(axum::http::header::CONTENT_TYPE, "application/json")],
serde_json::to_string(&body).unwrap(),
)
}
}