48 lines
1.3 KiB
Rust
48 lines
1.3 KiB
Rust
use axum::{
|
|
Json,
|
|
extract::{Extension, State},
|
|
http::StatusCode,
|
|
response::{IntoResponse, Response},
|
|
};
|
|
use serde_json::{Value, json};
|
|
|
|
use crate::{
|
|
api_response::json_success_body, http_error::AppError, request_context::RequestContext,
|
|
state::AppState,
|
|
};
|
|
|
|
pub async fn health_check(Extension(request_context): Extension<RequestContext>) -> Json<Value> {
|
|
json_success_body(
|
|
Some(&request_context),
|
|
json!({
|
|
"ok": true,
|
|
"service": "genarrative-api-server",
|
|
}),
|
|
)
|
|
}
|
|
|
|
pub async fn readiness_check(
|
|
State(state): State<AppState>,
|
|
Extension(request_context): Extension<RequestContext>,
|
|
) -> Response {
|
|
if state.is_ready() {
|
|
return json_success_body(
|
|
Some(&request_context),
|
|
json!({
|
|
"ok": true,
|
|
"ready": true,
|
|
"service": "genarrative-api-server",
|
|
}),
|
|
)
|
|
.into_response();
|
|
}
|
|
|
|
AppError::from_status(StatusCode::SERVICE_UNAVAILABLE)
|
|
.with_message("api-server 正在退出,不再接收新流量")
|
|
.with_details(json!({
|
|
"reason": "api_server_draining",
|
|
"ready": false,
|
|
}))
|
|
.into_response_with_context(Some(&request_context))
|
|
}
|