use axum::{http::StatusCode, response::IntoResponse, Json};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthResponse {
pub status: String,
pub version: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub uptime_secs: Option<u64>,
}
pub async fn health() -> impl IntoResponse {
let response = HealthResponse {
status: "ok".to_string(),
version: env!("CARGO_PKG_VERSION").to_string(),
uptime_secs: None,
};
(StatusCode::OK, Json(response))
}
pub async fn ready() -> impl IntoResponse {
let response = HealthResponse {
status: "ready".to_string(),
version: env!("CARGO_PKG_VERSION").to_string(),
uptime_secs: None,
};
(StatusCode::OK, Json(response))
}
pub async fn live() -> impl IntoResponse {
let response = HealthResponse {
status: "live".to_string(),
version: env!("CARGO_PKG_VERSION").to_string(),
uptime_secs: None,
};
(StatusCode::OK, Json(response))
}
pub async fn version() -> impl IntoResponse {
#[derive(Serialize)]
struct VersionInfo {
version: String,
build_time: String,
git_commit: String,
}
let info = VersionInfo {
version: env!("CARGO_PKG_VERSION").to_string(),
build_time: "unknown".to_string(), git_commit: "unknown".to_string(), };
(StatusCode::OK, Json(info))
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_health_endpoint() {
let response = health().await.into_response();
assert_eq!(response.status(), StatusCode::OK);
}
#[tokio::test]
async fn test_ready_endpoint() {
let response = ready().await.into_response();
assert_eq!(response.status(), StatusCode::OK);
}
#[tokio::test]
async fn test_live_endpoint() {
let response = live().await.into_response();
assert_eq!(response.status(), StatusCode::OK);
}
#[tokio::test]
async fn test_version_endpoint() {
let response = version().await.into_response();
assert_eq!(response.status(), StatusCode::OK);
}
}