use actix_web::{HttpResponse, get, web};
use sqlx::PgPool;
#[get("/health")]
pub async fn health() -> HttpResponse {
HttpResponse::Ok().json(serde_json::json!({"status": "ok"}))
}
#[get("/health-pg")]
pub async fn health_pg(pool: web::Data<PgPool>) -> HttpResponse {
match sqlx::query("SELECT 1").execute(pool.get_ref()).await {
Ok(_) => HttpResponse::Ok().json(serde_json::json!({"status": "ok", "db": "postgres"})),
Err(_) => HttpResponse::ServiceUnavailable()
.json(serde_json::json!({"status": "error", "db": "postgres"}))
}
}
#[get("/health-neo")]
pub async fn health_neo(graph: web::Data<neo4rs::Graph>) -> HttpResponse {
match graph.run(neo4rs::query("RETURN 1 AS _n")).await {
Ok(_) => HttpResponse::Ok().json(serde_json::json!({"status": "ok", "db": "neo4j"})),
Err(_) => HttpResponse::ServiceUnavailable()
.json(serde_json::json!({"status": "error", "db": "neo4j"}))
}
}
#[cfg(test)]
mod tests {
use super::*;
use actix_web::{App, test};
#[actix_web::test]
async fn health_always_returns_200() {
let app = test::init_service(App::new().service(health)).await;
let req = test::TestRequest::get().uri("/health").to_request();
let resp = test::call_service(&app, req).await;
assert_eq!(resp.status().as_u16(), 200u16);
}
}