How to Build a Health Check Endpoint in Rust

Web
Create a GET /health route in your Rust web server that returns a 200 OK status and a JSON status object to verify application availability.

Add a GET /health route to your server that returns a 200 OK status with a JSON body indicating the service is running.

use actix_web::{web, App, HttpResponse, HttpServer, Responder};

async fn health_check() -> impl Responder {
    HttpResponse::Ok().json({
        serde_json::json!({
            "status": "healthy",
            "timestamp": chrono::Utc::now().to_rfc3339()
        })
    })
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .route("/health", web::get().to(health_check))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

Ensure actix-web, serde, serde_json, and chrono are in your Cargo.toml dependencies.