mirror of
https://github.com/pcvolkmer/checkbar.git
synced 2025-04-19 11:06:50 +00:00
Move common check into trait with default implementation
This commit is contained in:
parent
e46dcabb01
commit
a76e3d8035
12
Cargo.lock
generated
12
Cargo.lock
generated
@ -20,6 +20,17 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "async-trait"
|
||||
version = "0.1.61"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "705339e0e4a9690e2908d2b3d049d85682cf19fbd5782494498fbf7003a6a282"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
@ -66,6 +77,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
name = "checkbar"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"chrono",
|
||||
"dirs",
|
||||
"regex",
|
||||
|
@ -6,6 +6,7 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
async-trait = "0.1"
|
||||
chrono = { version = "*", features = ["serde"] }
|
||||
dirs = "4"
|
||||
regex = "1.7"
|
||||
|
@ -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>,
|
||||
{
|
||||
#[async_trait]
|
||||
pub trait HttpBasedChecker {
|
||||
async fn check(&self) -> CheckResult {
|
||||
CheckResult {
|
||||
name: check_config.name.to_string(),
|
||||
state: match reqwest::get(check_config.url.as_str()).await {
|
||||
Ok(r) => f(r).await,
|
||||
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user