elif_http/server/
health.rs

1//! Health check endpoint implementation
2
3use crate::config::HttpConfig;
4use elif_core::container::IocContainer;
5use serde_json::json;
6use std::sync::Arc;
7
8/// Default health check handler
9pub async fn health_check_handler(
10    _container: Arc<IocContainer>,
11    _config: HttpConfig,
12) -> axum::response::Json<serde_json::Value> {
13    let response = json!({
14        "status": "healthy",
15        "framework": "Elif.rs",
16        "version": env!("CARGO_PKG_VERSION"),
17        "timestamp": std::time::SystemTime::now()
18            .duration_since(std::time::UNIX_EPOCH)
19            .unwrap()
20            .as_secs(),
21        "server": {
22            "ready": true,
23            "uptime": "N/A"
24        }
25    });
26
27    axum::response::Json(response)
28}
29
30/// Health check response structure
31#[derive(serde::Serialize)]
32pub struct HealthStatus {
33    pub status: String,
34    pub framework: String,
35    pub version: String,
36    pub timestamp: u64,
37    pub server: ServerStatus,
38}
39
40#[derive(serde::Serialize)]
41pub struct ServerStatus {
42    pub ready: bool,
43    pub uptime: String,
44}
45
46impl Default for HealthStatus {
47    fn default() -> Self {
48        Self {
49            status: "healthy".to_string(),
50            framework: "Elif.rs".to_string(),
51            version: env!("CARGO_PKG_VERSION").to_string(),
52            timestamp: std::time::SystemTime::now()
53                .duration_since(std::time::UNIX_EPOCH)
54                .unwrap()
55                .as_secs(),
56            server: ServerStatus {
57                ready: true,
58                uptime: "N/A".to_string(),
59            },
60        }
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67    use crate::testing::create_test_container;
68
69    #[tokio::test]
70    async fn test_health_check_handler() {
71        let container = create_test_container();
72        let config = HttpConfig::default();
73
74        let response = health_check_handler(container, config).await;
75        // Test that response is properly formatted JSON
76        assert!(response.0.get("status").is_some());
77        assert_eq!(response.0["status"], "healthy");
78    }
79
80    #[test]
81    fn test_health_status_default() {
82        let status = HealthStatus::default();
83        assert_eq!(status.status, "healthy");
84        assert_eq!(status.framework, "Elif.rs");
85        assert!(status.server.ready);
86    }
87}