mirror of
https://github.com/pcvolkmer/checkbar.git
synced 2025-04-19 19:16:50 +00:00
Extract function to remove duplicated code
This commit is contained in:
parent
e2f4e8463b
commit
e46dcabb01
@ -1,7 +1,7 @@
|
||||
use reqwest::Response;
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::checker::{CheckResult, CheckState};
|
||||
use crate::checker::{check_http_response, CheckResult, CheckState};
|
||||
use crate::config::CheckConfig;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -19,13 +19,7 @@ impl Checker<'_> {
|
||||
}
|
||||
|
||||
pub async fn check(&self) -> CheckResult {
|
||||
CheckResult {
|
||||
name: self.check_config.name.to_string(),
|
||||
state: match reqwest::get(self.check_config.url.as_str()).await {
|
||||
Ok(r) => Self::check_response(r).await,
|
||||
Err(_) => CheckState::Down,
|
||||
},
|
||||
}
|
||||
check_http_response(self.check_config, Self::check_response).await
|
||||
}
|
||||
|
||||
async fn check_response(response: Response) -> CheckState {
|
||||
|
@ -1,6 +1,6 @@
|
||||
use reqwest::Response;
|
||||
|
||||
use crate::checker::{CheckResult, CheckState};
|
||||
use crate::checker::{check_http_response, CheckResult, CheckState};
|
||||
use crate::config::CheckConfig;
|
||||
|
||||
pub struct Checker<'a> {
|
||||
@ -13,13 +13,7 @@ impl Checker<'_> {
|
||||
}
|
||||
|
||||
pub async fn check(&self) -> CheckResult {
|
||||
CheckResult {
|
||||
name: self.check_config.name.to_string(),
|
||||
state: match reqwest::get(self.check_config.url.as_str()).await {
|
||||
Ok(r) => Self::check_response(r).await,
|
||||
Err(_) => CheckState::Down,
|
||||
},
|
||||
}
|
||||
check_http_response(self.check_config, Self::check_response).await
|
||||
}
|
||||
|
||||
async fn check_response(response: Response) -> CheckState {
|
||||
|
@ -3,13 +3,15 @@ mod http;
|
||||
mod tcp;
|
||||
|
||||
use std::fmt::{Display, Formatter, Result};
|
||||
use std::future::Future;
|
||||
|
||||
use reqwest::Response;
|
||||
use serde_json::json;
|
||||
|
||||
pub use crate::checker::actuator::Checker as ActuatorChecker;
|
||||
pub use crate::checker::http::Checker as HttpChecker;
|
||||
pub use crate::checker::tcp::Checker as TcpChecker;
|
||||
use crate::config::Config;
|
||||
use crate::config::{CheckConfig, Config};
|
||||
|
||||
pub struct CheckResult {
|
||||
pub name: String,
|
||||
@ -43,3 +45,16 @@ pub enum CheckState {
|
||||
Warn,
|
||||
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,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user