summaryrefslogtreecommitdiff
path: root/src/health.rs
diff options
context:
space:
mode:
authorDawid Rycerz <dawid@rycerz.xyz>2025-07-30 19:26:07 +0300
committerDawid Rycerz <dawid@rycerz.xyz>2025-07-30 19:26:07 +0300
commit228879a2c390e08c227b7b1b2a5f44e1c34bf4e3 (patch)
treeb1af3a0008ead34805e267c5d9c356873952961b /src/health.rs
parent934fb31059da10fa843d96a10c37f181eaa89456 (diff)
feat: add automatic migrations
Diffstat (limited to 'src/health.rs')
-rw-r--r--src/health.rs59
1 files changed, 55 insertions, 4 deletions
diff --git a/src/health.rs b/src/health.rs
index c3ae80a..546d5d4 100644
--- a/src/health.rs
+++ b/src/health.rs
@@ -1,16 +1,67 @@
-use axum::{Json, response::IntoResponse};
+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", body = inline(HealthResponse))
+ (status = 200, description = "Health check response", body = serde_json::Value)
),
tag = "health"
)]
-pub async fn health_handler() -> impl IntoResponse {
- Json(json!({"status": "ok"}))
+pub async fn health_handler(
+ axum::extract::State(pool): axum::extract::State<Arc<SqlitePool>>,
+) -> Json<serde_json::Value> {
+ 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)]