Merge pull request 'feat: add /health endpoint for monitoring' (#3) from feat/health-endpoint into main

Reviewed-on: #3
This commit was merged in pull request #3.
This commit is contained in:
oc
2026-03-21 15:34:24 -07:00
+26
View File
@@ -16,6 +16,8 @@ use crate::{models::*, AppState};
pub fn router(state: AppState) -> Router {
Router::new()
// Health check (no auth)
.route("/health", get(health_check))
// Web UI
.route("/", get(dashboard))
.route("/login", get(login_page).post(login_post))
@@ -29,6 +31,30 @@ pub fn router(state: AppState) -> Router {
.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 ──────────────────────────────────────────────────────
fn session_user(jar: &CookieJar, state: &AppState) -> Option<String> {