mirror of
https://github.com/pcvolkmer/checkbar.git
synced 2025-04-19 19:16: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",
|
"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]]
|
[[package]]
|
||||||
name = "autocfg"
|
name = "autocfg"
|
||||||
version = "1.1.0"
|
version = "1.1.0"
|
||||||
@ -66,6 +77,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
|||||||
name = "checkbar"
|
name = "checkbar"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"async-trait",
|
||||||
"chrono",
|
"chrono",
|
||||||
"dirs",
|
"dirs",
|
||||||
"regex",
|
"regex",
|
||||||
|
@ -6,6 +6,7 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
async-trait = "0.1"
|
||||||
chrono = { version = "*", features = ["serde"] }
|
chrono = { version = "*", features = ["serde"] }
|
||||||
dirs = "4"
|
dirs = "4"
|
||||||
regex = "1.7"
|
regex = "1.7"
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
|
use async_trait::async_trait;
|
||||||
use reqwest::Response;
|
use reqwest::Response;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::checker::{check_http_response, CheckResult, CheckState};
|
use crate::checker::{CheckState, HttpBasedChecker};
|
||||||
use crate::config::CheckConfig;
|
use crate::config::CheckConfig;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@ -17,11 +18,10 @@ impl Checker<'_> {
|
|||||||
pub fn new(check_config: &CheckConfig) -> Checker {
|
pub fn new(check_config: &CheckConfig) -> Checker {
|
||||||
Checker { check_config }
|
Checker { check_config }
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn check(&self) -> CheckResult {
|
#[async_trait]
|
||||||
check_http_response(self.check_config, Self::check_response).await
|
impl HttpBasedChecker for Checker<'_> {
|
||||||
}
|
|
||||||
|
|
||||||
async fn check_response(response: Response) -> CheckState {
|
async fn check_response(response: Response) -> CheckState {
|
||||||
if response.status().is_success() {
|
if response.status().is_success() {
|
||||||
return match response.json::<ActuatorResponse>().await {
|
return match response.json::<ActuatorResponse>().await {
|
||||||
@ -31,4 +31,8 @@ impl Checker<'_> {
|
|||||||
}
|
}
|
||||||
CheckState::Warn
|
CheckState::Warn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_check_config(&self) -> &CheckConfig {
|
||||||
|
self.check_config
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
|
use async_trait::async_trait;
|
||||||
use reqwest::Response;
|
use reqwest::Response;
|
||||||
|
|
||||||
use crate::checker::{check_http_response, CheckResult, CheckState};
|
use crate::checker::{CheckState, HttpBasedChecker};
|
||||||
use crate::config::CheckConfig;
|
use crate::config::CheckConfig;
|
||||||
|
|
||||||
pub struct Checker<'a> {
|
pub struct Checker<'a> {
|
||||||
@ -11,11 +12,10 @@ impl Checker<'_> {
|
|||||||
pub fn new(check_config: &CheckConfig) -> Checker {
|
pub fn new(check_config: &CheckConfig) -> Checker {
|
||||||
Checker { check_config }
|
Checker { check_config }
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn check(&self) -> CheckResult {
|
#[async_trait]
|
||||||
check_http_response(self.check_config, Self::check_response).await
|
impl HttpBasedChecker for Checker<'_> {
|
||||||
}
|
|
||||||
|
|
||||||
async fn check_response(response: Response) -> CheckState {
|
async fn check_response(response: Response) -> CheckState {
|
||||||
if response.status().is_success() {
|
if response.status().is_success() {
|
||||||
CheckState::Up
|
CheckState::Up
|
||||||
@ -23,4 +23,8 @@ impl Checker<'_> {
|
|||||||
CheckState::Warn
|
CheckState::Warn
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_check_config(&self) -> &CheckConfig {
|
||||||
|
self.check_config
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,8 @@ mod actuator;
|
|||||||
mod http;
|
mod http;
|
||||||
mod tcp;
|
mod tcp;
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
use std::fmt::{Display, Formatter, Result};
|
use std::fmt::{Display, Formatter, Result};
|
||||||
use std::future::Future;
|
|
||||||
|
|
||||||
use reqwest::Response;
|
use reqwest::Response;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
@ -46,15 +46,19 @@ pub enum CheckState {
|
|||||||
Down,
|
Down,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn check_http_response<F>(check_config: &CheckConfig, f: fn(Response) -> F) -> CheckResult
|
#[async_trait]
|
||||||
where
|
pub trait HttpBasedChecker {
|
||||||
F: Future<Output = CheckState>,
|
async fn check(&self) -> CheckResult {
|
||||||
{
|
|
||||||
CheckResult {
|
CheckResult {
|
||||||
name: check_config.name.to_string(),
|
name: self.get_check_config().name.to_string(),
|
||||||
state: match reqwest::get(check_config.url.as_str()).await {
|
state: match reqwest::get(self.get_check_config().url.as_str()).await {
|
||||||
Ok(r) => f(r).await,
|
Ok(r) => Self::check_response(r).await,
|
||||||
Err(_) => CheckState::Down,
|
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 serde_json::json;
|
||||||
use tokio::task;
|
use tokio::task;
|
||||||
|
|
||||||
use crate::checker::{ActuatorChecker, CheckResult, HttpChecker, TcpChecker};
|
use crate::checker::{ActuatorChecker, CheckResult, HttpBasedChecker, HttpChecker, TcpChecker};
|
||||||
use crate::config::{CheckConfig, CheckType, Config};
|
use crate::config::{CheckConfig, CheckType, Config};
|
||||||
|
|
||||||
mod checker;
|
mod checker;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user