mirror of
https://github.com/stalwartlabs/mail-server.git
synced 2025-09-04 11:04:12 +08:00
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:
parent
98dce5d9ea
commit
cc46f73720
3 changed files with 47 additions and 11 deletions
|
@ -43,14 +43,20 @@ async fn main() -> std::io::Result<()> {
|
||||||
} else if let Ok(credentials) = std::env::var("CREDENTIALS") {
|
} else if let Ok(credentials) = std::env::var("CREDENTIALS") {
|
||||||
parse_credentials(&credentials)
|
parse_credentials(&credentials)
|
||||||
} else {
|
} else {
|
||||||
let credentials = rpassword::prompt_password(
|
if args.anonymous {
|
||||||
"\nEnter administrator credentials or press [ENTER] to use OAuth: ",
|
let credentials = "anonymous:".to_string();
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
if !credentials.is_empty() {
|
|
||||||
parse_credentials(&credentials)
|
parse_credentials(&credentials)
|
||||||
} else {
|
} 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,
|
timeout: args.timeout,
|
||||||
|
@ -172,7 +178,7 @@ async fn oauth(url: &str) -> Credentials {
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
pub enum Response<T> {
|
pub enum Response<T> {
|
||||||
Error(ManagementApiError),
|
Error(ManagementApiError),
|
||||||
Data { data: T },
|
Data { data: T }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
@ -281,7 +287,7 @@ impl Client {
|
||||||
Response::Error(error) => {
|
Response::Error(error) => {
|
||||||
eprintln!("Request failed: {error})");
|
eprintln!("Request failed: {error})");
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,9 @@ pub struct Cli {
|
||||||
/// Connection timeout in seconds
|
/// Connection timeout in seconds
|
||||||
#[clap(short, long)]
|
#[clap(short, long)]
|
||||||
pub timeout: Option<u64>,
|
pub timeout: Option<u64>,
|
||||||
|
/// Do not ask for credentials
|
||||||
|
#[clap(short, long)]
|
||||||
|
pub anonymous: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
|
@ -450,6 +453,12 @@ pub enum ServerCommands {
|
||||||
/// Prefix to filter configuration entries by
|
/// Prefix to filter configuration entries by
|
||||||
prefix: Option<String>,
|
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)]
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
|
||||||
|
|
|
@ -5,12 +5,11 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use prettytable::{Attr, Cell, Row, Table};
|
use prettytable::{Attr, Cell, Row, Table};
|
||||||
use reqwest::Method;
|
use reqwest::{Method, StatusCode};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
use crate::modules::Response;
|
use crate::modules::{Response, UnwrapResult};
|
||||||
|
|
||||||
use super::cli::{Client, ServerCommands};
|
use super::cli::{Client, ServerCommands};
|
||||||
|
|
||||||
|
@ -112,6 +111,28 @@ impl ServerCommands {
|
||||||
if results.len() == 1 { "" } else { "s" }
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue