Skip to content

Instantly share code, notes, and snippets.

@ahndmal
Last active August 5, 2025 10:23
Show Gist options
  • Select an option

  • Save ahndmal/b7c1c5c82268e62d3ef431abedf71def to your computer and use it in GitHub Desktop.

Select an option

Save ahndmal/b7c1c5c82268e62d3ef431abedf71def to your computer and use it in GitHub Desktop.
use axum::{Router, routing::get, Json};
use serde::Serialize;
use std::{sync::Arc, time::SystemTime};
#[derive(Serialize)]
struct Health {
status: &'static str,
uptime_seconds: u64,
}
async fn health_check(start_time: Arc<SystemTime>) -> Json<Health> {
let uptime = SystemTime::now()
.duration_since(*start_time)
.unwrap_or_default()
.as_secs();
Json(Health {
status: "ok",
uptime_seconds: uptime,
})
}
#[tokio::main]
async fn main() {
let start_time = Arc::new(SystemTime::now());
let app = Router::new().route(
"/health",
get({
let start_time = start_time.clone();
move || health_check(start_time.clone())
}),
);
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment