Last active
August 5, 2025 10:23
-
-
Save ahndmal/b7c1c5c82268e62d3ef431abedf71def to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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