use axum::Json; use serde_json::json; use sqlx::SqlitePool; use std::sync::Arc; use tracing::warn; use crate::migrations::MigrationManager; #[utoipa::path( get, path = "/health", responses( (status = 200, description = "Health check response", body = serde_json::Value) ), tag = "health" )] pub async fn health_handler( axum::extract::State(pool): axum::extract::State>, ) -> Json { let mut status = "ok"; let details; // Check database connectivity match sqlx::query("SELECT 1").execute(&*pool).await { Ok(_) => { // Check migration status match MigrationManager::check_migrations_status(&pool).await { Ok(migrations_ok) => { if migrations_ok { details = json!({ "database": "connected", "migrations": "up_to_date" }); } else { warn!("Health check: migrations are not up to date"); details = json!({ "database": "connected", "migrations": "pending" }); } } Err(e) => { warn!("Health check: failed to check migration status: {}", e); details = json!({ "database": "connected", "migrations": "unknown", "migration_error": e.to_string() }); } } } Err(e) => { status = "error"; details = json!({ "database": "disconnected", "error": e.to_string() }); } } Json(json!({ "status": status, "details": details })) } #[derive(utoipa::ToSchema, serde::Serialize)] pub struct HealthResponse { pub status: String, }