use std::collections::HashMap;
use std::sync::Arc;
use autumn_web::actuator::{HealthCheckOutput, HealthIndicator, HealthStatus};
use crate::client::DiscordClient;
pub struct DiscordHealth {
client: DiscordClient,
}
impl DiscordHealth {
#[must_use]
pub const fn new(client: DiscordClient) -> Self {
Self { client }
}
pub async fn check_now(&self) -> crate::error::Result<String> {
self.client.check_token().await.map(|info| info.name)
}
}
impl HealthIndicator for DiscordHealth {
fn check(&self) -> futures::future::BoxFuture<'_, HealthCheckOutput> {
let client = self.client.clone();
Box::pin(async move {
match client.check_token().await {
Ok(info) => {
let mut details = HashMap::new();
details.insert(
"application".to_owned(),
serde_json::Value::String(info.name),
);
HealthCheckOutput {
status: HealthStatus::Up,
details,
}
}
Err(err) => {
let mut details = HashMap::new();
details.insert(
"error".to_owned(),
serde_json::Value::String(err.to_string()),
);
HealthCheckOutput {
status: HealthStatus::Down,
details,
}
}
}
})
}
}
#[must_use]
pub fn indicator(client: &DiscordClient) -> Arc<DiscordHealth> {
Arc::new(DiscordHealth::new(client.clone()))
}
#[cfg(test)]
mod tests;