Skip to content

Commit

Permalink
health check (#688)
Browse files Browse the repository at this point in the history
Creates a health check route which just returns 200 OK.
  • Loading branch information
0xForerunner authored Feb 9, 2024
1 parent 34daf4b commit e7d240a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ pub async fn bind_from_listener(
listener: TcpListener,
) -> anyhow::Result<()> {
let router = Router::new()
// Operate on identity commitments
.route("/verifySemaphoreProof", post(verify_semaphore_proof))
.route("/inclusionProof", post(inclusion_proof))
.route("/insertIdentity", post(insert_identity))
Expand All @@ -162,6 +163,8 @@ pub async fn bind_from_listener(
.route("/addBatchSize", post(add_batch_size))
.route("/removeBatchSize", post(remove_batch_size))
.route("/listBatchSizes", get(list_batch_sizes))
// Health check, return 200 OK
.route("/health", get(()))
.layer(middleware::from_fn(
custom_middleware::api_metrics_layer::middleware,
))
Expand Down
24 changes: 22 additions & 2 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,13 +598,14 @@ pub async fn spawn_app(config: Config) -> anyhow::Result<(JoinHandle<()>, Socket
let listener = TcpListener::bind(server_config.address).expect("Failed to bind random port");
let local_addr = listener.local_addr()?;

// For our tests to work we need the tree to be initialised so we
// need to sleep for a short moment.
// For our tests to work we need the tree to be initialized.
while app.tree_state().is_err() {
trace!("Waiting for the tree to be initialized");
tokio::time::sleep(Duration::from_millis(250)).await;
}

check_health(&local_addr).await?;

let app = spawn({
async move {
info!("App thread starting");
Expand All @@ -618,6 +619,25 @@ pub async fn spawn_app(config: Config) -> anyhow::Result<(JoinHandle<()>, Socket
Ok((app, local_addr))
}

pub async fn check_health(socket_addr: &SocketAddr) -> anyhow::Result<()> {
let uri = format!("http://{}", socket_addr);
let client = Client::new();
let req = Request::builder()
.method("GET")
.uri(uri.to_owned() + "/health")
.body(Body::empty())
.expect("Failed to create health check hyper::Body");
let response = client
.request(req)
.await
.context("Failed to execute health check request.")?;
if !response.status().is_success() {
anyhow::bail!("Health check failed");
}

Ok(())
}

#[derive(Deserialize, Serialize, Debug)]
struct CompiledContract {
abi: Abi,
Expand Down

0 comments on commit e7d240a

Please sign in to comment.