Skip to content

Commit 3141296

Browse files
committed
feat: [torrust#508] add new binary HTTP health check
It makes a request to an HTTP endpoint to check that the service is healthy.
1 parent 345261a commit 3141296

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[package]
2+
default-run = "torrust-tracker"
23
name = "torrust-tracker"
34
readme = "README.md"
45

src/bin/http_health_check.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//! Minimal `curl` or `wget` to be used for container health checks.
2+
//!
3+
//! It's convenient to avoid using third-party libraries because:
4+
//!
5+
//! - They are harder to maintain.
6+
//! - They introduce new attack vectors.
7+
use std::{env, process};
8+
9+
#[tokio::main]
10+
async fn main() {
11+
let args: Vec<String> = env::args().collect();
12+
if args.len() != 2 {
13+
eprintln!("Usage: cargo run --bin health_check <HEALTH_URL>");
14+
eprintln!("Example: cargo run --bin http_health_check http://localhost:1212/api/v1/stats?token=MyAccessToken");
15+
std::process::exit(1);
16+
}
17+
18+
println!("Health check ...");
19+
20+
let url = &args[1].clone();
21+
22+
match reqwest::get(url).await {
23+
Ok(response) => {
24+
if response.status().is_success() {
25+
println!("STATUS: {}", response.status());
26+
process::exit(0);
27+
} else {
28+
println!("Non-success status received.");
29+
process::exit(1);
30+
}
31+
}
32+
Err(err) => {
33+
println!("ERROR: {err}");
34+
process::exit(1);
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)