This commit is contained in:
2025-10-31 15:55:55 +01:00
parent 48bed9a9fe
commit d14d263e8f
+33 -3
View File
@@ -1,6 +1,9 @@
use axum::{http::StatusCode, response::Json};
use axum::{extract::State, http::StatusCode, response::Json};
use diesel::RunQueryDsl;
use serde_json::json;
use crate::state::AppState;
#[derive(utoipa::OpenApi)]
#[openapi(paths(crate::routes::health::health_check))]
pub struct HealthApiDoc;
@@ -11,6 +14,33 @@ pub struct HealthApiDoc;
responses((status = 200, description = "Service is healthy")),
tag = "Health"
)]
pub async fn health_check() -> (StatusCode, Json<serde_json::Value>) {
(StatusCode::OK, Json(json!({ "status": "ok" })))
pub async fn health_check(State(state): State<AppState>) -> (StatusCode, Json<serde_json::Value>) {
let database_ok = match state.db_unscoped() {
Ok(mut conn) => diesel::sql_query("SELECT 1")
.execute(&mut conn)
.map(|_| true)
.unwrap_or_else(|err| {
tracing::error!(error = ?err, "health check database ping failed");
false
}),
Err(err) => {
tracing::error!(error = ?err, "health check database connection failed");
false
}
};
let status = if database_ok {
StatusCode::OK
} else {
StatusCode::SERVICE_UNAVAILABLE
};
let payload = json!({
"status": if database_ok { "ok" } else { "error" },
"checks": {
"database": if database_ok { "ok" } else { "unavailable" }
}
});
(status, Json(payload))
}