2019-04-30 07:53:13 +00:00
|
|
|
extern crate actix_web;
|
2019-05-01 02:49:22 +00:00
|
|
|
extern crate env_logger;
|
2019-04-30 07:53:13 +00:00
|
|
|
extern crate reqwest;
|
|
|
|
extern crate ron;
|
|
|
|
extern crate serde;
|
2019-05-01 02:49:22 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate tera;
|
2019-04-30 07:53:13 +00:00
|
|
|
|
2019-05-01 05:28:57 +00:00
|
|
|
mod config;
|
2019-05-01 02:49:22 +00:00
|
|
|
mod utils;
|
|
|
|
|
2019-05-01 05:28:57 +00:00
|
|
|
use self::config::*;
|
2019-05-01 02:49:22 +00:00
|
|
|
use self::utils::EpochTimestamp;
|
2019-04-30 07:53:13 +00:00
|
|
|
use actix_web::{
|
2019-05-01 02:49:22 +00:00
|
|
|
error::ErrorInternalServerError,
|
|
|
|
middleware::Logger,
|
|
|
|
web::{resource, Data},
|
|
|
|
App, Error as WebError, HttpResponse, HttpServer, Result as WebResult,
|
2019-04-30 07:53:13 +00:00
|
|
|
};
|
2019-04-30 06:19:58 +00:00
|
|
|
use reqwest::{Client, Url, UrlError};
|
2019-04-30 05:44:26 +00:00
|
|
|
use ron::de::from_str;
|
2019-05-01 05:28:57 +00:00
|
|
|
use serde::Serialize;
|
2019-04-30 07:53:13 +00:00
|
|
|
use std::{
|
|
|
|
error::Error,
|
|
|
|
fs::read_to_string,
|
2019-04-30 19:27:15 +00:00
|
|
|
sync::{Arc, Mutex, MutexGuard},
|
2019-04-30 07:53:13 +00:00
|
|
|
};
|
2019-05-01 02:49:22 +00:00
|
|
|
use tera::{Context, Tera};
|
2019-04-30 05:44:26 +00:00
|
|
|
|
2019-05-01 02:49:22 +00:00
|
|
|
#[derive(Clone, Serialize)]
|
|
|
|
pub struct Status {
|
2019-04-30 05:44:26 +00:00
|
|
|
status: u8,
|
|
|
|
location: String,
|
|
|
|
domain: String,
|
|
|
|
endpoint: String,
|
|
|
|
error: Option<String>,
|
|
|
|
}
|
|
|
|
|
2019-05-01 02:49:22 +00:00
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct FetchResults {
|
|
|
|
last_update: EpochTimestamp,
|
2019-04-30 07:53:13 +00:00
|
|
|
refresh_time: u64,
|
|
|
|
config: Config,
|
|
|
|
statuses: Vec<Status>,
|
|
|
|
}
|
|
|
|
|
|
|
|
type StatusState = Arc<Mutex<FetchResults>>;
|
|
|
|
|
2019-05-01 02:49:22 +00:00
|
|
|
fn index(tmpl: Data<Tera>, state: Data<StatusState>) -> WebResult<HttpResponse, WebError> {
|
2019-04-30 19:27:15 +00:00
|
|
|
let state = update_state(state.lock().unwrap());
|
2019-05-01 02:49:22 +00:00
|
|
|
let mut ctx = Context::new();
|
|
|
|
ctx.insert("results", &*state);
|
2019-05-01 04:53:14 +00:00
|
|
|
let s = tmpl.render("index.html", &ctx).map_err(|e| {
|
|
|
|
println!("{:?}", e);
|
|
|
|
ErrorInternalServerError("Template error")
|
|
|
|
})?;
|
2019-05-01 02:49:22 +00:00
|
|
|
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
2019-04-30 07:53:13 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 02:49:22 +00:00
|
|
|
fn json_endpoint(state: Data<StatusState>) -> HttpResponse {
|
2019-04-30 19:27:15 +00:00
|
|
|
let state = update_state(state.lock().unwrap());
|
2019-05-01 02:49:22 +00:00
|
|
|
HttpResponse::Ok().json(&state.statuses)
|
2019-04-30 19:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn update_state(mut state: MutexGuard<FetchResults>) -> MutexGuard<FetchResults> {
|
2019-05-01 02:49:22 +00:00
|
|
|
if EpochTimestamp::now() - state.last_update >= state.refresh_time {
|
|
|
|
state.last_update = EpochTimestamp::now();
|
2019-04-30 08:20:01 +00:00
|
|
|
state.statuses = update_status(&state.config);
|
|
|
|
}
|
|
|
|
|
2019-04-30 19:27:15 +00:00
|
|
|
state
|
2019-04-30 08:20:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 05:44:26 +00:00
|
|
|
fn main() -> Result<(), Box<Error>> {
|
|
|
|
let config = from_str::<Config>(&read_to_string("./endstat_conf.ron")?)?;
|
2019-05-01 02:49:22 +00:00
|
|
|
let bind_addr = config.bind_address.clone();
|
|
|
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
|
|
|
env_logger::init();
|
2019-04-30 06:47:35 +00:00
|
|
|
|
2019-05-01 02:49:22 +00:00
|
|
|
HttpServer::new(move || {
|
2019-04-30 19:27:15 +00:00
|
|
|
let state = Arc::from(Mutex::from(FetchResults {
|
2019-05-01 02:49:22 +00:00
|
|
|
last_update: EpochTimestamp::now(),
|
|
|
|
refresh_time: config.refresh_time.clone(),
|
2019-04-30 19:27:15 +00:00
|
|
|
config: config.clone(),
|
|
|
|
statuses: update_status(&config),
|
|
|
|
}));
|
2019-05-01 02:49:22 +00:00
|
|
|
let tera = compile_templates!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*"));
|
|
|
|
|
|
|
|
App::new()
|
|
|
|
.data(state)
|
|
|
|
.data(tera)
|
|
|
|
.wrap(Logger::default())
|
|
|
|
.service(resource("/").to(index))
|
|
|
|
.service(resource("/api").to(json_endpoint))
|
2019-04-30 07:53:13 +00:00
|
|
|
})
|
2019-05-01 02:49:22 +00:00
|
|
|
.bind(&bind_addr)?
|
|
|
|
.run()?;
|
2019-04-30 06:47:35 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-04-30 05:44:26 +00:00
|
|
|
|
2019-04-30 07:53:13 +00:00
|
|
|
fn update_status(config: &Config) -> Vec<Status> {
|
2019-04-30 06:47:35 +00:00
|
|
|
let client = Client::new();
|
2019-04-30 05:44:26 +00:00
|
|
|
let mut results: Vec<Status> = vec![];
|
|
|
|
|
2019-04-30 07:53:13 +00:00
|
|
|
for website_conf in &config.websites {
|
|
|
|
for endpoint in &website_conf.endpoints {
|
2019-05-01 04:53:14 +00:00
|
|
|
results.push(get_result(website_conf, &client, endpoint));
|
2019-04-30 05:44:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-30 06:47:35 +00:00
|
|
|
results
|
2019-04-30 05:44:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 05:28:57 +00:00
|
|
|
fn get_result(website_conf: &WebsiteConfig, client: &Client, endpoint: &EndpointConfig) -> Status {
|
2019-05-01 04:53:14 +00: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;
|
2019-05-01 05:28:57 +00:00
|
|
|
let mut error = None;
|
2019-05-01 04:53:14 +00:00
|
|
|
|
|
|
|
if !does_code_match {
|
2019-05-01 05:28:57 +00:00
|
|
|
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,
|
2019-05-01 04:53:14 +00:00
|
|
|
res_body.len(),
|
|
|
|
body.len()
|
2019-05-01 05:28:57 +00:00
|
|
|
)
|
|
|
|
} 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,
|
2019-05-01 04:53:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => Status {
|
|
|
|
status: 2,
|
|
|
|
location: url,
|
|
|
|
domain: website_conf.label.clone(),
|
|
|
|
endpoint: label,
|
|
|
|
error: Some(format!("{}", e)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-30 06:19:58 +00:00
|
|
|
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())
|
|
|
|
}
|