aro-fletch 1.0.0

Fletch ORM persistence adapter for the Aro web framework
Documentation
//! Database health check endpoint.
//!
//! Provides `GET /health/db` that verifies database connectivity and returns
//! pool statistics.

use axum::http::StatusCode;
use axum::response::IntoResponse;
use sqlx::Database;

use aro_web::dep::Dep;

/// Handler for `GET /health/db`.
///
/// Attempts to acquire a connection from the pool. Returns 200 with pool stats
/// if healthy, or 503 if the database is unreachable.
#[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(),
        )
    }
}