Skip to content

Commit 1bccbdb

Browse files
move healthcheck to a separate binary
1 parent f71dd21 commit 1bccbdb

File tree

4 files changed

+46
-51
lines changed

4 files changed

+46
-51
lines changed

Cargo.lock

+32-32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ FROM scratch
1919
WORKDIR /
2020

2121
COPY --from=server-builder /build/target/release/mcping /usr/bin/mcping
22+
COPY --from=server-builder /build/target/release/mcping-healthcheck /usr/bin/mcping-healthcheck
2223
COPY --from=client-builder /assets/ /var/www/mcping/
2324

2425
ENV ASSET_DIR="/var/www/mcping/"
2526

26-
HEALTHCHECK --interval=5s --timeout=5s --retries=5 CMD ["/usr/bin/mcping", "healthcheck", "http://127.0.0.1:8080"]
27+
HEALTHCHECK --interval=5s --timeout=5s --retries=5 CMD ["/usr/bin/mcping-healthcheck", "http://127.0.0.1:8080"]
2728
ENTRYPOINT ["/usr/bin/mcping"]

src/bin/mcping-healthcheck.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#[tokio::main(flavor = "current_thread")]
2+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
3+
let Some(url) = std::env::args().nth(1) else {
4+
return Err("`mcping-healthcheck` requires exactly one argument.".into());
5+
};
6+
if std::env::args().len() != 2 {
7+
return Err("`mcping-healthcheck` requires exactly one argument.".into());
8+
}
9+
reqwest::get(url).await?.error_for_status()?;
10+
println!("Health check succeeded");
11+
Ok(())
12+
}

src/main.rs

-18
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ static JSON_CONTENT_TYPE: HeaderValue = HeaderValue::from_static("application/js
5959
#[allow(clippy::too_many_lines)]
6060
#[tokio::main]
6161
async fn main() -> Result<(), Box<dyn std::error::Error>> {
62-
if let Some(subcmd) = std::env::args().nth(1).as_deref() {
63-
return check_health(subcmd).await;
64-
}
6562
tracing_subscriber::fmt()
6663
.with_max_level(Level::TRACE)
6764
.json()
@@ -193,21 +190,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
193190
Ok(())
194191
}
195192

196-
async fn check_health(subcmd: &str) -> Result<(), Box<dyn std::error::Error>> {
197-
if subcmd != "healthcheck" {
198-
return Err("The only valid subcommand is `healthcheck`".into());
199-
}
200-
let Some(url) = std::env::args().nth(2) else {
201-
return Err("`healthcheck` requires exactly one argument.".into());
202-
};
203-
if std::env::args().len() != 3 {
204-
return Err("`healthcheck` requires exactly one argument.".into());
205-
}
206-
reqwest::get(url).await?.error_for_status()?;
207-
println!("Health check succeeded");
208-
Ok(())
209-
}
210-
211193
fn get_csp() -> ContentSecurityPolicy {
212194
ContentSecurityPolicy::new_empty()
213195
.upgrade_insecure_requests(true)

0 commit comments

Comments
 (0)