Skip to main content

soma_api/
probes.rs

1use axum::{
2    extract::State,
3    response::{IntoResponse, Response},
4    Json,
5};
6use serde_json::{json, Value};
7use soma_http_api::probe::{liveness_response, readiness_response};
8
9use crate::ApiState;
10
11/// `GET /health` — liveness probe (unauthenticated).
12pub async fn health() -> impl IntoResponse {
13    tracing::debug!("health probe");
14    liveness_response()
15}
16
17/// `GET /readyz` — readiness probe (unauthenticated).
18///
19/// Unlike `/health` (pure liveness: "the process is up"), this probes the
20/// upstream dependency and returns `503 Service Unavailable` when it is
21/// unreachable, so orchestrators only route traffic once the server can serve it.
22pub async fn readyz(State(state): State<ApiState>) -> Response {
23    let result = state.application().readiness().await;
24    if let Err(error) = &result {
25        tracing::warn!(%error, "readiness probe failed");
26    }
27    readiness_response(result)
28}
29
30/// `GET /status` — local runtime status (unauthenticated, redacts secrets).
31pub async fn status(State(state): State<ApiState>) -> Response {
32    Json(status_body(state.server_name())).into_response()
33}
34
35fn status_body(server_name: &str) -> Value {
36    json!({
37        "status": "ok",
38        "server": server_name,
39        "version": env!("CARGO_PKG_VERSION"),
40        "transport": "http",
41    })
42}
43
44#[cfg(test)]
45#[path = "probes_tests.rs"]
46mod tests;