endstat/src/updater.rs

107 lines
3.4 KiB
Rust
Raw Normal View History

2019-05-01 13:59:55 -07:00
use crate::{
config::*,
handlers::{EndpointStatus, StatusGroup},
State,
};
use chrono::prelude::Utc;
2019-05-01 14:24:18 -07:00
use reqwest::{Client, RedirectPolicy, Url, UrlError};
2019-05-01 15:02:38 -07:00
use std::time::Duration;
2019-05-01 11:13:46 -07:00
pub fn update_state(state: State) {
let new_statuses = { Some(update_status(&state.read().unwrap().config)) };
let mut write_state = state.try_write().expect("Could not unlock");
2019-05-01 13:15:18 -07:00
write_state.timestamp = Utc::now();
write_state.timestamp_str = Utc::now().format("%Y-%m-%d %H:%M:%S").to_string();
2019-05-01 13:59:55 -07:00
write_state.groups = new_statuses.unwrap();
2019-05-01 11:13:46 -07:00
}
2019-05-01 13:59:55 -07:00
pub fn update_status(config: &Config) -> Vec<StatusGroup> {
let mut results: Vec<StatusGroup> = Vec::with_capacity(config.websites.len());
2019-05-01 11:13:46 -07:00
for website_conf in &config.websites {
2019-05-01 13:59:55 -07:00
let mut group = Vec::with_capacity(website_conf.endpoints.len());
2019-05-01 11:13:46 -07:00
for endpoint in &website_conf.endpoints {
2019-05-01 15:02:38 -07:00
let mut client_builder = Client::builder().timeout(Some(Duration::from_secs(5)));
2019-05-01 14:24:18 -07:00
if let Some(false) = endpoint.follow_redirects {
client_builder = client_builder.redirect(RedirectPolicy::none());
}
let client = client_builder.build().unwrap();
2019-05-01 13:59:55 -07:00
group.push(get_result(website_conf, &client, endpoint));
2019-05-01 11:13:46 -07:00
}
2019-05-01 13:59:55 -07:00
results.push(StatusGroup {
label: website_conf.label.clone(),
endpoints: group,
});
2019-05-01 11:13:46 -07:00
}
results
}
2019-05-01 13:59:55 -07:00
fn get_result(
website_conf: &WebsiteConfig,
client: &Client,
endpoint: &EndpointConfig,
) -> EndpointStatus {
2019-05-01 11:13:46 -07:00
let (label, path, port, code, body) = get_endpoint_info(endpoint.clone());
let url = get_url(&website_conf.base, &path, port).expect("reading config");
let ping_result = client.get(&url).send();
match ping_result {
Ok(mut res) => {
let res_body = res.text().expect("could not get body of request");
let does_code_match = res.status() == code;
let does_body_match = body.is_empty() || res_body == body;
let mut error = None;
if !does_code_match {
error = Some(format!(
"Status code mismatch: {} != {}.",
res.status().as_u16(),
code
));
}
if !does_body_match {
error = Some(if let Some(msg) = error {
format!(
"{} Body mismatch: {} != {}.",
msg,
res_body.len(),
body.len()
)
} else {
format!("Body mismatch: {} != {}.", res_body.len(), body.len())
});
}
if error.is_some() {
EndpointStatus::warn(url, label, error)
} else {
EndpointStatus::ok(url, label)
2019-05-01 11:13:46 -07:00
}
}
Err(e) => {
if let Some(true) = endpoint.should_err {
EndpointStatus::ok(url, label)
} else {
EndpointStatus::error(url, label, Some(format!("{}", e)))
}
}
2019-05-01 11:13:46 -07:00
}
}
2019-05-01 14:16:54 -07:00
fn get_url(base: &Option<String>, path: &String, port: Option<u16>) -> Result<String, UrlError> {
let mut url = if let Some(base) = base {
Url::parse(base)?.join(path)?
} else {
Url::parse(path)?
};
2019-05-01 11:13:46 -07:00
if let Err(e) = url.set_port(port) {
println!("{:?}", e);
}
Ok(url.into_string())
}