From 551a2c4c3853ba7b1bc7a7f10c9cf312f7d66693 Mon Sep 17 00:00:00 2001 From: "mae.kasza" <26093674+MaeIsBad@users.noreply.github.com> Date: Fri, 8 Mar 2024 12:38:52 +0100 Subject: [PATCH] Bundle healthcheck with server --- .github/workflows/cd.yml | 6 +--- Cargo.toml | 1 - src/bin/healthcheck.rs | 43 ------------------------ src/{bin/localauth0.rs => main.rs} | 52 +++++++++++++++++++++++++++++- 4 files changed, 52 insertions(+), 50 deletions(-) delete mode 100644 src/bin/healthcheck.rs rename src/{bin/localauth0.rs => main.rs} (69%) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index a251ac4..ec509b2 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -38,7 +38,6 @@ jobs: name: localauth0-${{ matrix.arch }} path: | ./target/${{ matrix.arch }}/release/localauth0 - ./target/${{ matrix.arch }}/release/healthcheck build-web: runs-on: ubuntu-latest @@ -79,18 +78,15 @@ jobs: for arch in amd64 arm64; do rust_arch="$([ "$arch" = "arm64" ] && echo aarch64 || echo x86_64)-unknown-linux-musl" localauth0_binary="./localauth0-$rust_arch/localauth0" - healthcheck_binary="./localauth0-$rust_arch/localauth0" chmod +x "$localauth0_binary" - chmod +x "$healthcheck_binary" ctr="$(buildah from --arch $arch $base)" buildah copy "$ctr" "$localauth0_binary" "/localauth0" - buildah copy "$ctr" "$healthcheck_binary" "/healthcheck" buildah copy "$ctr" ./web/ /web/dist buildah config \ --env 'RUST_LOG=error,localauth0=info' \ --cmd '["/localauth0"]' \ - --healthcheck '["/healthcheck"]' \ + --healthcheck 'CMD /localauth0 healthcheck' \ --healthcheck-interval 0.1s \ --healthcheck-timeout 2s \ --healthcheck-start-period 2s \ diff --git a/Cargo.toml b/Cargo.toml index 73f06ff..b85ab7e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,6 @@ edition = "2021" name = "localauth0" version = "0.7.0" -default-run = "localauth0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/bin/healthcheck.rs b/src/bin/healthcheck.rs deleted file mode 100644 index 02313d3..0000000 --- a/src/bin/healthcheck.rs +++ /dev/null @@ -1,43 +0,0 @@ -use localauth0::config::Config; - -async fn is_endpoint_healthy(client: &reqwest::Client, endpoint: String) -> bool { - let status = client.get(&endpoint).send().await.map(|r| r.error_for_status()); - - match status { - Ok(Ok(_)) => { - println!("{endpoint} OK"); - true - } - _ => { - println!("{endpoint} ERROR {status:?}"); - false - } - } -} - -fn main() -> Result<(), ()> { - tokio::runtime::Builder::new_current_thread() - .enable_all() - .build() - .unwrap() - .block_on(async { - let config = Config::load_or_default(); - - let client = reqwest::Client::builder() - .danger_accept_invalid_certs(true) - .build() - .unwrap(); - - let endpoint_http = format!("http://127.0.0.1:{}/healthcheck", config.http().port()); - let endpoint_https = format!("https://127.0.0.1:{}/healthcheck", config.https().port()); - - let http_healthy = is_endpoint_healthy(&client, endpoint_http).await; - let https_healthy = is_endpoint_healthy(&client, endpoint_https).await; - - if http_healthy && https_healthy { - Ok(()) - } else { - Err(()) - } - }) -} diff --git a/src/bin/localauth0.rs b/src/main.rs similarity index 69% rename from src/bin/localauth0.rs rename to src/main.rs index d1034eb..0a91c3e 100644 --- a/src/bin/localauth0.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use std::error::Error; use std::time::Duration; use actix_files::{Files, NamedFile}; @@ -16,8 +17,57 @@ use localauth0::{controller, APP_NAME}; // Singleton logger. Used to free user from manually passing Logger objects around. static LOGGER_GUARD: GuardLoggerCell = GuardLoggerCell::new(); +fn main() -> Result<(), Box> { + match std::env::args().skip(1).next().as_deref() { + Some("healthcheck") => Ok(healthcheck()?), + _ => Ok(server()?), + } +} + +async fn is_endpoint_healthy(client: &reqwest::Client, endpoint: String) -> bool { + let status = client.get(&endpoint).send().await.map(|r| r.error_for_status()); + + match status { + Ok(Ok(_)) => { + println!("{endpoint} OK"); + true + } + _ => { + println!("{endpoint} ERROR {status:?}"); + false + } + } +} + +fn healthcheck() -> Result<(), String> { + tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap() + .block_on(async { + let config = Config::load_or_default(); + + let client = reqwest::Client::builder() + .danger_accept_invalid_certs(true) + .build() + .unwrap(); + + let endpoint_http = format!("http://127.0.0.1:{}/healthcheck", config.http().port()); + let endpoint_https = format!("https://127.0.0.1:{}/healthcheck", config.https().port()); + + let http_healthy = is_endpoint_healthy(&client, endpoint_http).await; + let https_healthy = is_endpoint_healthy(&client, endpoint_https).await; + + if http_healthy && https_healthy { + Ok(()) + } else { + Err("healthcheck failed".to_string()) + } + }) +} + #[actix_web::main] -async fn main() -> std::io::Result<()> { +async fn server() -> std::io::Result<()> { LOGGER_GUARD .set(prima_rs_logger::term_guard(APP_NAME)) .expect("Cannot set global logger guard");