Add healthcheck w/o credentials to stalwart-cli (#1880)

* Add healthcheck w/o credentials to stalwart-cli

* Rename enum Response<T> variant

The HealthcheckStatus identifier is not as generic, as it should be.
The Response data format, generated by the function `into_http_response()`
for `JsonProblemResponse`

* Aktualisieren von database.rs

* Healthcheck with direct call
This commit is contained in:
Codekloeppler 2025-08-09 22:13:26 +02:00 committed by GitHub
parent 98dce5d9ea
commit cc46f73720
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 47 additions and 11 deletions

View file

@ -43,14 +43,20 @@ async fn main() -> std::io::Result<()> {
} else if let Ok(credentials) = std::env::var("CREDENTIALS") {
parse_credentials(&credentials)
} else {
let credentials = rpassword::prompt_password(
"\nEnter administrator credentials or press [ENTER] to use OAuth: ",
)
.unwrap();
if !credentials.is_empty() {
if args.anonymous {
let credentials = "anonymous:".to_string();
parse_credentials(&credentials)
} else {
oauth(&url).await
let credentials = rpassword::prompt_password(
"\nEnter administrator credentials or press [ENTER] to use OAuth: ",
)
.unwrap();
if !credentials.is_empty() {
parse_credentials(&credentials)
} else {
oauth(&url).await
}
}
},
timeout: args.timeout,
@ -172,7 +178,7 @@ async fn oauth(url: &str) -> Credentials {
#[serde(untagged)]
pub enum Response<T> {
Error(ManagementApiError),
Data { data: T },
Data { data: T }
}
#[derive(Deserialize)]
@ -281,7 +287,7 @@ impl Client {
Response::Error(error) => {
eprintln!("Request failed: {error})");
std::process::exit(1);
}
},
}
}
}

View file

@ -25,6 +25,9 @@ pub struct Cli {
/// Connection timeout in seconds
#[clap(short, long)]
pub timeout: Option<u64>,
/// Do not ask for credentials
#[clap(short, long)]
pub anonymous: bool,
}
#[derive(Subcommand)]
@ -450,6 +453,12 @@ pub enum ServerCommands {
/// Prefix to filter configuration entries by
prefix: Option<String>,
},
/// Perform Healthcheck
Healthcheck {
/// Status `ready` (default) or `live` to check for
check: Option<String>
},
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]

View file

@ -5,12 +5,11 @@
*/
use std::collections::HashMap;
use prettytable::{Attr, Cell, Row, Table};
use reqwest::Method;
use reqwest::{Method, StatusCode};
use serde_json::Value;
use crate::modules::Response;
use crate::modules::{Response, UnwrapResult};
use super::cli::{Client, ServerCommands};
@ -112,6 +111,28 @@ impl ServerCommands {
if results.len() == 1 { "" } else { "s" }
);
}
ServerCommands::Healthcheck { check } => {
let response = reqwest::get(
format!("{}/healthz/{}",
client.url,
check.unwrap_or("ready".to_string()))
)
.await
.unwrap();
match response.status() {
StatusCode::OK => {
eprintln!("Success")
},
_ => {
eprintln!(
"Request failed: {}",
response.text().await.unwrap_result("fetch text")
);
std::process::exit(1);
}
}
}
}
}
}