cleaned up imports, refactored code
This commit is contained in:
parent
af79f6d2a3
commit
fef8fd66c7
3 changed files with 97 additions and 108 deletions
107
src/handlers.rs
107
src/handlers.rs
|
@ -1,22 +1,18 @@
|
||||||
use crate::config::*;
|
use crate::{config::*, utils::EpochTimestamp, State};
|
||||||
use crate::utils::EpochTimestamp;
|
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
error::ErrorInternalServerError, web::Data, Error as WebError, HttpResponse,
|
error::ErrorInternalServerError, web::Data, Error as WebError, HttpResponse,
|
||||||
Result as WebResult,
|
Result as WebResult,
|
||||||
};
|
};
|
||||||
use reqwest::{Client, Url, UrlError};
|
|
||||||
|
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::sync::{Arc, RwLock};
|
|
||||||
use tera::{Context, Tera};
|
use tera::{Context, Tera};
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Default, Debug)]
|
#[derive(Clone, Serialize, Default, Debug)]
|
||||||
pub struct Status {
|
pub struct Status {
|
||||||
status: u8,
|
pub status: u8,
|
||||||
location: String,
|
pub location: String,
|
||||||
domain: String,
|
pub domain: String,
|
||||||
endpoint: String,
|
pub endpoint: String,
|
||||||
error: Option<String>,
|
pub error: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Debug)]
|
#[derive(Serialize, Debug)]
|
||||||
|
@ -27,8 +23,6 @@ pub struct QueryResults {
|
||||||
pub statuses: Vec<Status>,
|
pub statuses: Vec<Status>,
|
||||||
}
|
}
|
||||||
|
|
||||||
type State = Arc<RwLock<QueryResults>>;
|
|
||||||
|
|
||||||
pub fn index(tmpl: Data<Tera>, state: Data<State>) -> WebResult<HttpResponse, WebError> {
|
pub fn index(tmpl: Data<Tera>, state: Data<State>) -> WebResult<HttpResponse, WebError> {
|
||||||
let state = state.read().unwrap();
|
let state = state.read().unwrap();
|
||||||
let mut ctx = Context::new();
|
let mut ctx = Context::new();
|
||||||
|
@ -44,92 +38,3 @@ pub fn json_endpoint(state: Data<State>) -> HttpResponse {
|
||||||
let state = state.read().unwrap();
|
let state = state.read().unwrap();
|
||||||
HttpResponse::Ok().json(&state.statuses)
|
HttpResponse::Ok().json(&state.statuses)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_state(state: State) {
|
|
||||||
let mut new_timestamp = None;
|
|
||||||
let mut new_statuses = None;
|
|
||||||
{
|
|
||||||
let read_state = state.read().unwrap();
|
|
||||||
if EpochTimestamp::now() - read_state.last_update >= read_state.refresh_time {
|
|
||||||
new_timestamp = Some(EpochTimestamp::now());
|
|
||||||
new_statuses = Some(update_status(&read_state.config));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if new_timestamp.is_some() {
|
|
||||||
let mut write_state = state.try_write().expect("Could not unlock");
|
|
||||||
write_state.last_update = new_timestamp.unwrap();
|
|
||||||
write_state.statuses = new_statuses.unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update_status(config: &Config) -> Vec<Status> {
|
|
||||||
let client = Client::new();
|
|
||||||
let mut results: Vec<Status> = vec![];
|
|
||||||
|
|
||||||
for website_conf in &config.websites {
|
|
||||||
for endpoint in &website_conf.endpoints {
|
|
||||||
results.push(get_result(website_conf, &client, endpoint));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
results
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_result(website_conf: &WebsiteConfig, client: &Client, endpoint: &EndpointConfig) -> Status {
|
|
||||||
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())
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Status {
|
|
||||||
status: if error.is_some() { 1 } else { 0 },
|
|
||||||
location: url,
|
|
||||||
domain: website_conf.label.clone(),
|
|
||||||
endpoint: label,
|
|
||||||
error,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => Status {
|
|
||||||
status: 2,
|
|
||||||
location: url,
|
|
||||||
domain: website_conf.label.clone(),
|
|
||||||
endpoint: label,
|
|
||||||
error: Some(format!("{}", e)),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_url(base: &String, path: &String, port: Option<u16>) -> Result<String, UrlError> {
|
|
||||||
let mut url = Url::parse(base)?.join(path)?;
|
|
||||||
if let Err(e) = url.set_port(port) {
|
|
||||||
println!("{:?}", e);
|
|
||||||
}
|
|
||||||
Ok(url.into_string())
|
|
||||||
}
|
|
||||||
|
|
17
src/main.rs
17
src/main.rs
|
@ -10,11 +10,10 @@ extern crate tera;
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
mod handlers;
|
mod handlers;
|
||||||
|
mod updater;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
use self::config::*;
|
use self::{config::*, handlers::*, updater::update_state, utils::EpochTimestamp};
|
||||||
use self::handlers::*;
|
|
||||||
use self::utils::EpochTimestamp;
|
|
||||||
use actix::System;
|
use actix::System;
|
||||||
use actix_web::{middleware::Logger, web::resource, App, HttpServer};
|
use actix_web::{middleware::Logger, web::resource, App, HttpServer};
|
||||||
use ron::de::from_str;
|
use ron::de::from_str;
|
||||||
|
@ -23,8 +22,12 @@ use std::{
|
||||||
sync::{Arc, RwLock},
|
sync::{Arc, RwLock},
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
use tokio::prelude::{Future, Stream};
|
use tokio::{
|
||||||
use tokio::timer::Interval;
|
prelude::{Future, Stream},
|
||||||
|
timer::Interval,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub type State = Arc<RwLock<QueryResults>>;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
System::run(move || {
|
System::run(move || {
|
||||||
|
@ -36,7 +39,7 @@ fn main() {
|
||||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let state = Arc::new(RwLock::new(QueryResults {
|
let state: State = Arc::new(RwLock::new(QueryResults {
|
||||||
last_update: EpochTimestamp::now(),
|
last_update: EpochTimestamp::now(),
|
||||||
refresh_time: config.refresh_time.clone(),
|
refresh_time: config.refresh_time.clone(),
|
||||||
config: config.clone(),
|
config: config.clone(),
|
||||||
|
@ -60,7 +63,7 @@ fn main() {
|
||||||
.start();
|
.start();
|
||||||
|
|
||||||
tokio::spawn(
|
tokio::spawn(
|
||||||
Interval::new_interval(Duration::from_millis(5000))
|
Interval::new_interval(Duration::from_secs(config.refresh_time))
|
||||||
.for_each(move |_| {
|
.for_each(move |_| {
|
||||||
let state = Arc::clone(&clone_state);
|
let state = Arc::clone(&clone_state);
|
||||||
update_state(state);
|
update_state(state);
|
||||||
|
|
81
src/updater.rs
Normal file
81
src/updater.rs
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
use crate::{config::*, handlers::Status, utils::EpochTimestamp, State};
|
||||||
|
use reqwest::{Client, Url, UrlError};
|
||||||
|
|
||||||
|
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");
|
||||||
|
write_state.last_update = EpochTimestamp::now();
|
||||||
|
write_state.statuses = new_statuses.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_status(config: &Config) -> Vec<Status> {
|
||||||
|
let client = Client::new();
|
||||||
|
let mut results: Vec<Status> = vec![];
|
||||||
|
|
||||||
|
for website_conf in &config.websites {
|
||||||
|
for endpoint in &website_conf.endpoints {
|
||||||
|
results.push(get_result(website_conf, &client, endpoint));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
results
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_result(website_conf: &WebsiteConfig, client: &Client, endpoint: &EndpointConfig) -> Status {
|
||||||
|
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())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Status {
|
||||||
|
status: if error.is_some() { 1 } else { 0 },
|
||||||
|
location: url,
|
||||||
|
domain: website_conf.label.clone(),
|
||||||
|
endpoint: label,
|
||||||
|
error,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => Status {
|
||||||
|
status: 2,
|
||||||
|
location: url,
|
||||||
|
domain: website_conf.label.clone(),
|
||||||
|
endpoint: label,
|
||||||
|
error: Some(format!("{}", e)),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_url(base: &String, path: &String, port: Option<u16>) -> Result<String, UrlError> {
|
||||||
|
let mut url = Url::parse(base)?.join(path)?;
|
||||||
|
if let Err(e) = url.set_port(port) {
|
||||||
|
println!("{:?}", e);
|
||||||
|
}
|
||||||
|
Ok(url.into_string())
|
||||||
|
}
|
Loading…
Reference in a new issue