Skip to content

Commit

Permalink
feat: [#508] add new binary HTTP health check
Browse files Browse the repository at this point in the history
It makes a request to an HTTP endpoint to check that the service is
healthy.
  • Loading branch information
josecelano committed Nov 24, 2023
1 parent f1c7ccc commit 0ef4e34
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[package]
default-run = "torrust-tracker"
name = "torrust-tracker"
readme = "README.md"

Expand Down
37 changes: 37 additions & 0 deletions src/bin/http_health_check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! Minimal `curl` or `wget` to be used for container health checks.
//!
//! It's convenient to avoid using third-party libraries because:
//!
//! - They are harder to maintain.
//! - They introduce new attack vectors.
use std::{env, process};

#[tokio::main]
async fn main() {
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
eprintln!("Usage: cargo run --bin health_check <HEALTH_URL>");
eprintln!("Example: cargo run --bin http_health_check http://localhost:1212/api/v1/stats?token=MyAccessToken");
std::process::exit(1);
}

println!("Health check ...");

let url = &args[1].clone();

match reqwest::get(url).await {
Ok(response) => {
if response.status().is_success() {
println!("STATUS: {}", response.status());
process::exit(0);
} else {
println!("Non-success status received.");
process::exit(1);
}
}
Err(err) => {
println!("ERROR: {err}");
process::exit(1);
}
}
}

0 comments on commit 0ef4e34

Please sign in to comment.