mirror of
https://github.com/pcvolkmer/checkbar.git
synced 2025-07-02 06:22:53 +00:00
Move common check into trait with default implementation
This commit is contained in:
@ -1,7 +1,8 @@
|
||||
use async_trait::async_trait;
|
||||
use reqwest::Response;
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::checker::{check_http_response, CheckResult, CheckState};
|
||||
use crate::checker::{CheckState, HttpBasedChecker};
|
||||
use crate::config::CheckConfig;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -17,11 +18,10 @@ impl Checker<'_> {
|
||||
pub fn new(check_config: &CheckConfig) -> Checker {
|
||||
Checker { check_config }
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn check(&self) -> CheckResult {
|
||||
check_http_response(self.check_config, Self::check_response).await
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl HttpBasedChecker for Checker<'_> {
|
||||
async fn check_response(response: Response) -> CheckState {
|
||||
if response.status().is_success() {
|
||||
return match response.json::<ActuatorResponse>().await {
|
||||
@ -31,4 +31,8 @@ impl Checker<'_> {
|
||||
}
|
||||
CheckState::Warn
|
||||
}
|
||||
|
||||
fn get_check_config(&self) -> &CheckConfig {
|
||||
self.check_config
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
use async_trait::async_trait;
|
||||
use reqwest::Response;
|
||||
|
||||
use crate::checker::{check_http_response, CheckResult, CheckState};
|
||||
use crate::checker::{CheckState, HttpBasedChecker};
|
||||
use crate::config::CheckConfig;
|
||||
|
||||
pub struct Checker<'a> {
|
||||
@ -11,11 +12,10 @@ impl Checker<'_> {
|
||||
pub fn new(check_config: &CheckConfig) -> Checker {
|
||||
Checker { check_config }
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn check(&self) -> CheckResult {
|
||||
check_http_response(self.check_config, Self::check_response).await
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl HttpBasedChecker for Checker<'_> {
|
||||
async fn check_response(response: Response) -> CheckState {
|
||||
if response.status().is_success() {
|
||||
CheckState::Up
|
||||
@ -23,4 +23,8 @@ impl Checker<'_> {
|
||||
CheckState::Warn
|
||||
}
|
||||
}
|
||||
|
||||
fn get_check_config(&self) -> &CheckConfig {
|
||||
self.check_config
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,8 @@ mod actuator;
|
||||
mod http;
|
||||
mod tcp;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use std::fmt::{Display, Formatter, Result};
|
||||
use std::future::Future;
|
||||
|
||||
use reqwest::Response;
|
||||
use serde_json::json;
|
||||
@ -46,15 +46,19 @@ pub enum CheckState {
|
||||
Down,
|
||||
}
|
||||
|
||||
async fn check_http_response<F>(check_config: &CheckConfig, f: fn(Response) -> F) -> CheckResult
|
||||
where
|
||||
F: Future<Output = CheckState>,
|
||||
{
|
||||
CheckResult {
|
||||
name: check_config.name.to_string(),
|
||||
state: match reqwest::get(check_config.url.as_str()).await {
|
||||
Ok(r) => f(r).await,
|
||||
Err(_) => CheckState::Down,
|
||||
},
|
||||
#[async_trait]
|
||||
pub trait HttpBasedChecker {
|
||||
async fn check(&self) -> CheckResult {
|
||||
CheckResult {
|
||||
name: self.get_check_config().name.to_string(),
|
||||
state: match reqwest::get(self.get_check_config().url.as_str()).await {
|
||||
Ok(r) => Self::check_response(r).await,
|
||||
Err(_) => CheckState::Down,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
async fn check_response(response: Response) -> CheckState;
|
||||
|
||||
fn get_check_config(&self) -> &CheckConfig;
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
use tokio::task;
|
||||
|
||||
use crate::checker::{ActuatorChecker, CheckResult, HttpChecker, TcpChecker};
|
||||
use crate::checker::{ActuatorChecker, CheckResult, HttpBasedChecker, HttpChecker, TcpChecker};
|
||||
use crate::config::{CheckConfig, CheckType, Config};
|
||||
|
||||
mod checker;
|
||||
|
Reference in New Issue
Block a user