Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /health endpoint #1989

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions crates/client-api/src/routes/health.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::{ControlStateDelegate, NodeDelegate};
use axum::extract::State;
use axum::response::IntoResponse;
use http::StatusCode;

static VERSION: &str = env!("CARGO_PKG_VERSION");
static PACKAGE_NAME: &str = env!("CARGO_PKG_NAME");

pub async fn health<S: ControlStateDelegate + NodeDelegate>(
State(ctx): State<S>,
) -> axum::response::Result<impl IntoResponse> {
let nodes: Vec<u64> = ctx
.get_nodes()
.map_err(|_| {
(
StatusCode::INTERNAL_SERVER_ERROR,
"Couldn't connect to the control database",
)
})?
.iter()
.map(|n| n.id)
.collect();
let schedulable = !ctx
.get_node_by_id(
ctx.get_node_id()
.ok_or((StatusCode::INTERNAL_SERVER_ERROR, "Can't get node id"))?,
)
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Couldn't get node info"))?
.map(|n| n.unschedulable)
.unwrap_or(false);

Ok(serde_json::json!({
"package_name": PACKAGE_NAME,
"version": VERSION,
"nodes": nodes,
"schedulable": schedulable,
})
.to_string())
}

pub fn router<S>() -> axum::Router<S>
where
S: ControlStateDelegate + NodeDelegate + Clone + 'static,
{
use axum::routing::get;
axum::Router::new().route("/", get(health::<S>))
}
1 change: 1 addition & 0 deletions crates/client-api/src/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod database;
pub mod energy;
pub mod health;
pub mod identity;
pub mod metrics;
pub mod prometheus;
Expand Down
5 changes: 3 additions & 2 deletions crates/standalone/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use http::header::{ACCEPT, AUTHORIZATION};
use tower_http::cors::{Any, CorsLayer};

use spacetimedb_client_api::{
routes::{database, energy, identity, metrics, prometheus},
routes::{database, energy, health, identity, metrics, prometheus},
ControlStateDelegate, NodeDelegate,
};

Expand All @@ -19,7 +19,8 @@ where
.nest("/identity", identity::router(ctx.clone()))
.nest("/energy", energy::router())
.nest("/prometheus", prometheus::router())
.nest("/metrics", metrics::router());
.nest("/metrics", metrics::router())
.nest("/health", health::router());

let cors = CorsLayer::new()
.allow_headers([AUTHORIZATION, ACCEPT])
Expand Down
Loading