Skip to content

Commit

Permalink
Bundle healthcheck with server
Browse files Browse the repository at this point in the history
  • Loading branch information
MaeIsBad committed Mar 8, 2024
1 parent 21630d0 commit 551a2c4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 50 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 \
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
43 changes: 0 additions & 43 deletions src/bin/healthcheck.rs

This file was deleted.

52 changes: 51 additions & 1 deletion src/bin/localauth0.rs → src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::error::Error;
use std::time::Duration;

use actix_files::{Files, NamedFile};
Expand All @@ -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<dyn Error>> {
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");
Expand Down

0 comments on commit 551a2c4

Please sign in to comment.