feat: add /health endpoint for monitoring #3

Merged
oc merged 1 commits from feat/health-endpoint into main 2026-03-21 15:34:24 -07:00
Showing only changes of commit a0a9a1024d - Show all commits
+26
View File
@@ -16,6 +16,8 @@ use crate::{models::*, AppState};
pub fn router(state: AppState) -> Router { pub fn router(state: AppState) -> Router {
Router::new() Router::new()
// Health check (no auth)
.route("/health", get(health_check))
// Web UI // Web UI
.route("/", get(dashboard)) .route("/", get(dashboard))
.route("/login", get(login_page).post(login_post)) .route("/login", get(login_page).post(login_post))
@@ -29,6 +31,30 @@ pub fn router(state: AppState) -> Router {
.with_state(state) .with_state(state)
} }
// ── Health Check ─────────────────────────────────────────────────
async fn health_check(State(state): State<AppState>) -> impl IntoResponse {
// Verify the database is accessible with a simple query
let db_ok = {
let db = state.db.lock().unwrap();
db.query_row("SELECT 1", [], |_| Ok(())).is_ok()
};
if db_ok {
(
StatusCode::OK,
Json(serde_json::json!({"ok": true, "status": "live"})),
)
.into_response()
} else {
(
StatusCode::SERVICE_UNAVAILABLE,
Json(serde_json::json!({"ok": false, "status": "database unreachable"})),
)
.into_response()
}
}
// ── Helpers ────────────────────────────────────────────────────── // ── Helpers ──────────────────────────────────────────────────────
fn session_user(jar: &CookieJar, state: &AppState) -> Option<String> { fn session_user(jar: &CookieJar, state: &AppState) -> Option<String> {