forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [torrust#508] add new binary HTTP health check
It makes a request to an HTTP endpoint to check that the service is healthy.
- Loading branch information
1 parent
16d6a47
commit 66d9921
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |