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