From a76e3d80353b6c92ff0bb75e224057ad55561881 Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Sun, 15 Jan 2023 03:30:57 +0100 Subject: [PATCH] Move common check into trait with default implementation --- Cargo.lock | 12 ++++++++++++ Cargo.toml | 1 + src/checker/actuator.rs | 14 +++++++++----- src/checker/http.rs | 14 +++++++++----- src/checker/mod.rs | 26 +++++++++++++++----------- src/main.rs | 2 +- 6 files changed, 47 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 873b128..f30d834 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/Cargo.toml b/Cargo.toml index 1b00ea5..49b9f4d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/checker/actuator.rs b/src/checker/actuator.rs index 4f72733..6dcf356 100644 --- a/src/checker/actuator.rs +++ b/src/checker/actuator.rs @@ -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::().await { @@ -31,4 +31,8 @@ impl Checker<'_> { } CheckState::Warn } + + fn get_check_config(&self) -> &CheckConfig { + self.check_config + } } diff --git a/src/checker/http.rs b/src/checker/http.rs index b3274ec..2520066 100644 --- a/src/checker/http.rs +++ b/src/checker/http.rs @@ -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 + } } diff --git a/src/checker/mod.rs b/src/checker/mod.rs index 9da5d27..898c20a 100644 --- a/src/checker/mod.rs +++ b/src/checker/mod.rs @@ -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(check_config: &CheckConfig, f: fn(Response) -> F) -> CheckResult -where - F: Future, -{ - 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; } diff --git a/src/main.rs b/src/main.rs index 608a295..8f2206b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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;