use axum::{
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
use crate::api::model::ErrorResponse;
#[utoipa::path(
get,
path = "/health",
responses(
(status = 200, description = "Everything is working fine"),
(status = 503, description = "Text generation inference is down", body = ErrorResponse, example = json!(ErrorResponse { error: String::from("unhealthy"), error_type: Some(String::from("healthcheck")) })),
),
tag = "Text Generation Inference"
)]
pub async fn get_health_handler() -> impl IntoResponse {
if check_server_health() {
Response::builder()
.status(StatusCode::OK)
.body("Everything is working fine".into())
.unwrap()
} else {
let error_response = ErrorResponse {
error: "unhealthy".into(),
error_type: Some("healthcheck".into()),
};
Json(error_response).into_response()
}
}
fn check_server_health() -> bool {
true }
#[cfg(test)]
mod tests {
use super::*;
use axum::{
body::{to_bytes, Body},
http::{Response, StatusCode},
};
#[tokio::test]
async fn test_get_health_handler() {
let response: Response<Body> = get_health_handler().await.into_response();
assert_eq!(response.status(), StatusCode::OK);
let body_limit = 1024 * 1024; let body_bytes = to_bytes(response.into_body(), body_limit).await.unwrap();
let body = String::from_utf8(body_bytes.to_vec()).unwrap();
assert_eq!(body, "Everything is working fine");
}
}