Skip to content

Building 70 Projects ranging from beginner to advanced so anyone can — learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company 👇

815 B
Raw Blame
// ©AngelaMos | 2026
// health.rs
//
// Health check endpoint
//
// check executes a SELECT 1 probe against the PostgreSQL
// pool and returns a JSON HealthResponse with status "ok"
// and database connectivity as "connected" or
// "disconnected".
//
// Connects to:
//   state.rs - AppState.db

use axum::Json;
use axum::extract::State;
use serde::Serialize;

use crate::state::AppState;

#[derive(Serialize)]
pub(crate) struct HealthResponse {
    status: &'static str,
    database: &'static str,
}

pub async fn check(State(state): State<AppState>) -> Json<HealthResponse> {
    let db_status = match sqlx::query("SELECT 1").execute(&state.db).await {
        Ok(_) => "connected",
        Err(_) => "disconnected",
    };

    Json(HealthResponse {
        status: "ok",
        database: db_status,
    })
}