more refactoring
This commit is contained in:
parent
9a9d87038a
commit
1428959602
3 changed files with 63 additions and 64 deletions
33
src/config.rs
Normal file
33
src/config.rs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||||
|
pub struct EndpointConfig {
|
||||||
|
pub label: Option<String>,
|
||||||
|
pub endpoint: Option<String>,
|
||||||
|
pub port: Option<u16>,
|
||||||
|
pub code: Option<u16>,
|
||||||
|
pub body: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||||
|
pub struct WebsiteConfig {
|
||||||
|
pub label: String,
|
||||||
|
pub base: String,
|
||||||
|
pub endpoints: Vec<EndpointConfig>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||||
|
pub struct Config {
|
||||||
|
pub refresh_time: u64,
|
||||||
|
pub bind_address: String,
|
||||||
|
pub websites: Vec<WebsiteConfig>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_endpoint_info(endpoint: EndpointConfig) -> (String, String, Option<u16>, u16, String) {
|
||||||
|
let path = endpoint.endpoint.unwrap_or_default();
|
||||||
|
let label = endpoint.label.unwrap_or_else(|| path.clone());
|
||||||
|
let code = endpoint.code.unwrap_or_else(|| 200);
|
||||||
|
let body = endpoint.body.unwrap_or_default();
|
||||||
|
|
||||||
|
(label, path, endpoint.port, code, body)
|
||||||
|
}
|
76
src/main.rs
76
src/main.rs
|
@ -6,8 +6,10 @@ extern crate serde;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate tera;
|
extern crate tera;
|
||||||
|
|
||||||
|
mod config;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
|
use self::config::*;
|
||||||
use self::utils::EpochTimestamp;
|
use self::utils::EpochTimestamp;
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
error::ErrorInternalServerError,
|
error::ErrorInternalServerError,
|
||||||
|
@ -17,7 +19,7 @@ use actix_web::{
|
||||||
};
|
};
|
||||||
use reqwest::{Client, Url, UrlError};
|
use reqwest::{Client, Url, UrlError};
|
||||||
use ron::de::from_str;
|
use ron::de::from_str;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::Serialize;
|
||||||
use std::{
|
use std::{
|
||||||
error::Error,
|
error::Error,
|
||||||
fs::read_to_string,
|
fs::read_to_string,
|
||||||
|
@ -25,29 +27,6 @@ use std::{
|
||||||
};
|
};
|
||||||
use tera::{Context, Tera};
|
use tera::{Context, Tera};
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
|
||||||
struct EndpointConf {
|
|
||||||
label: Option<String>,
|
|
||||||
endpoint: Option<String>,
|
|
||||||
port: Option<u16>,
|
|
||||||
code: Option<u16>,
|
|
||||||
body: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
|
||||||
struct WebsiteConf {
|
|
||||||
label: String,
|
|
||||||
base: String,
|
|
||||||
endpoints: Vec<EndpointConf>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
|
||||||
struct Config {
|
|
||||||
refresh_time: u64,
|
|
||||||
bind_address: String,
|
|
||||||
websites: Vec<WebsiteConf>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize)]
|
#[derive(Clone, Serialize)]
|
||||||
pub struct Status {
|
pub struct Status {
|
||||||
status: u8,
|
status: u8,
|
||||||
|
@ -133,7 +112,7 @@ fn update_status(config: &Config) -> Vec<Status> {
|
||||||
results
|
results
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_result(website_conf: &WebsiteConf, client: &Client, endpoint: &EndpointConf) -> Status {
|
fn get_result(website_conf: &WebsiteConfig, client: &Client, endpoint: &EndpointConfig) -> Status {
|
||||||
let (label, path, port, code, body) = get_endpoint_info(endpoint.clone());
|
let (label, path, port, code, body) = get_endpoint_info(endpoint.clone());
|
||||||
let url = get_url(&website_conf.base, &path, port).expect("reading config");
|
let url = get_url(&website_conf.base, &path, port).expect("reading config");
|
||||||
let ping_result = client.get(&url).send();
|
let ping_result = client.get(&url).send();
|
||||||
|
@ -143,39 +122,35 @@ fn get_result(website_conf: &WebsiteConf, client: &Client, endpoint: &EndpointCo
|
||||||
let res_body = res.text().expect("could not get body of request");
|
let res_body = res.text().expect("could not get body of request");
|
||||||
let does_code_match = res.status() == code;
|
let does_code_match = res.status() == code;
|
||||||
let does_body_match = body.is_empty() || res_body == body;
|
let does_body_match = body.is_empty() || res_body == body;
|
||||||
|
let mut error = None;
|
||||||
|
|
||||||
if !does_code_match {
|
if !does_code_match {
|
||||||
Status {
|
error = Some(format!(
|
||||||
status: 1,
|
"Status code mismatch: {} != {}!",
|
||||||
location: url,
|
|
||||||
domain: website_conf.label.clone(),
|
|
||||||
endpoint: label,
|
|
||||||
error: Some(format!(
|
|
||||||
"Status code mismatch! {} != {}",
|
|
||||||
res.status().as_u16(),
|
res.status().as_u16(),
|
||||||
code
|
code
|
||||||
)),
|
));
|
||||||
}
|
}
|
||||||
} else if !does_body_match {
|
|
||||||
Status {
|
if !does_body_match {
|
||||||
status: 1,
|
error = Some(if let Some(msg) = error {
|
||||||
location: url,
|
format!(
|
||||||
domain: website_conf.label.clone(),
|
"{} Body mismatch! {} != {}",
|
||||||
endpoint: label,
|
msg,
|
||||||
error: Some(format!(
|
|
||||||
"Body mismatch! {} != {}",
|
|
||||||
res_body.len(),
|
res_body.len(),
|
||||||
body.len()
|
body.len()
|
||||||
)),
|
)
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
|
format!("Body mismatch! {} != {}", res_body.len(), body.len())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Status {
|
Status {
|
||||||
status: 0,
|
status: if error.is_some() { 1 } else { 0 },
|
||||||
location: url,
|
location: url,
|
||||||
domain: website_conf.label.clone(),
|
domain: website_conf.label.clone(),
|
||||||
endpoint: label,
|
endpoint: label,
|
||||||
error: None,
|
error,
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => Status {
|
Err(e) => Status {
|
||||||
|
@ -195,12 +170,3 @@ fn get_url(base: &String, path: &String, port: Option<u16>) -> Result<String, Ur
|
||||||
}
|
}
|
||||||
Ok(url.into_string())
|
Ok(url.into_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_endpoint_info(endpoint: EndpointConf) -> (String, String, Option<u16>, u16, String) {
|
|
||||||
let path = endpoint.endpoint.unwrap_or_default();
|
|
||||||
let label = endpoint.label.unwrap_or_else(|| path.clone());
|
|
||||||
let code = endpoint.code.unwrap_or_else(|| 200);
|
|
||||||
let body = endpoint.body.unwrap_or_default();
|
|
||||||
|
|
||||||
(label, path, endpoint.port, code, body)
|
|
||||||
}
|
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
display: flex;
|
display: flex;
|
||||||
background-color: #424242;
|
background-color: #424242;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
margin: 1rem;
|
margin: 1rem 0;
|
||||||
padding: 1rem;
|
padding: 1rem;
|
||||||
border-radius: 1rem;
|
border-radius: 1rem;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue