use std::sync::Arc;
use axum::{extract::State, http::StatusCode, Json};
use serde_json::{json, Value};
use crate::AppState;
const VERSION: &str = env!("CARGO_PKG_VERSION");
pub async fn healthz() -> (StatusCode, Json<Value>) {
(
StatusCode::OK,
Json(json!({
"status": "ok",
"version": VERSION,
})),
)
}
pub async fn readyz(State(state): State<Arc<AppState>>) -> (StatusCode, Json<Value>) {
let smtp_ok = check_smtp_reachable(&state).await;
if smtp_ok {
(
StatusCode::OK,
Json(json!({
"status": "ready",
"smtp": "ok",
})),
)
} else {
(
StatusCode::SERVICE_UNAVAILABLE,
Json(json!({
"status": "not_ready",
"smtp": "unavailable",
})),
)
}
}
async fn check_smtp_reachable(state: &AppState) -> bool {
let cfg = state.config();
let addr = format!("{}:{}", cfg.smtp.host, cfg.smtp.port);
tokio::time::timeout(
std::time::Duration::from_secs(2),
tokio::net::TcpStream::connect(&addr),
)
.await
.map(|result| result.is_ok())
.unwrap_or(false)
}