Compare commits
4 commits
1428959602
...
d92a5b7004
Author | SHA1 | Date | |
---|---|---|---|
d92a5b7004 | |||
92036fe55f | |||
53b4cd70b0 | |||
cef4ce19f0 |
6 changed files with 240 additions and 149 deletions
49
Cargo.lock
generated
49
Cargo.lock
generated
|
@ -1,5 +1,30 @@
|
||||||
# This file is automatically @generated by Cargo.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
|
[[package]]
|
||||||
|
name = "actix"
|
||||||
|
version = "0.8.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"actix-rt 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"actix_derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"derive_more 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"futures 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"hashbrown 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"tokio-executor 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"tokio-tcp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"tokio-timer 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"trust-dns-resolver 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "actix-codec"
|
name = "actix-codec"
|
||||||
version = "0.1.2"
|
version = "0.1.2"
|
||||||
|
@ -215,6 +240,16 @@ dependencies = [
|
||||||
"syn 0.15.33 (registry+https://github.com/rust-lang/crates.io-index)",
|
"syn 0.15.33 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "actix_derive"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2 0.4.29 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"syn 0.15.33 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "adler32"
|
name = "adler32"
|
||||||
version = "1.0.3"
|
version = "1.0.3"
|
||||||
|
@ -465,6 +500,15 @@ dependencies = [
|
||||||
"cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
"cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-channel"
|
||||||
|
version = "0.3.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "crossbeam-deque"
|
name = "crossbeam-deque"
|
||||||
version = "0.7.1"
|
version = "0.7.1"
|
||||||
|
@ -607,8 +651,10 @@ dependencies = [
|
||||||
name = "endstat"
|
name = "endstat"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"actix 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"actix-web 1.0.0-beta.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"actix-web 1.0.0-beta.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"reqwest 0.9.15 (registry+https://github.com/rust-lang/crates.io-index)",
|
"reqwest 0.9.15 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"ron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"ron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)",
|
"serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
@ -2275,6 +2321,7 @@ dependencies = [
|
||||||
]
|
]
|
||||||
|
|
||||||
[metadata]
|
[metadata]
|
||||||
|
"checksum actix 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "409b193241782089260e5567aa8ac607a7607153613dbe8a15170ed3b29984fd"
|
||||||
"checksum actix-codec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9f2c11af4b06dc935d8e1b1491dad56bfb32febc49096a91e773f8535c176453"
|
"checksum actix-codec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9f2c11af4b06dc935d8e1b1491dad56bfb32febc49096a91e773f8535c176453"
|
||||||
"checksum actix-connect 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0dc9fb88787e5904e5030cae7d395f9908c2118ed655e48905f37febcad9a653"
|
"checksum actix-connect 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0dc9fb88787e5904e5030cae7d395f9908c2118ed655e48905f37febcad9a653"
|
||||||
"checksum actix-http 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "23f726f551edcf2ddfd9d56261aedede5337e5ed1eee17b66304b7c9f62458ca"
|
"checksum actix-http 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "23f726f551edcf2ddfd9d56261aedede5337e5ed1eee17b66304b7c9f62458ca"
|
||||||
|
@ -2287,6 +2334,7 @@ dependencies = [
|
||||||
"checksum actix-utils 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "80b12b95a3550c49b8f75e80341608bceaa6c544b0c754a0a6ffcf89bdd6d723"
|
"checksum actix-utils 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "80b12b95a3550c49b8f75e80341608bceaa6c544b0c754a0a6ffcf89bdd6d723"
|
||||||
"checksum actix-web 1.0.0-beta.2 (registry+https://github.com/rust-lang/crates.io-index)" = "07bedc2058b6fb05ef5e600653141e51ea588b68f8e7a82358e098965deaae48"
|
"checksum actix-web 1.0.0-beta.2 (registry+https://github.com/rust-lang/crates.io-index)" = "07bedc2058b6fb05ef5e600653141e51ea588b68f8e7a82358e098965deaae48"
|
||||||
"checksum actix-web-codegen 0.1.0-beta.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b26f9ce2dff34bda98b3c5b3ec2467a8f1bf08c69b0ae8ff02bc7d74d6af9d84"
|
"checksum actix-web-codegen 0.1.0-beta.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b26f9ce2dff34bda98b3c5b3ec2467a8f1bf08c69b0ae8ff02bc7d74d6af9d84"
|
||||||
|
"checksum actix_derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0bf5f6d7bf2d220ae8b4a7ae02a572bb35b7c4806b24049af905ab8110de156c"
|
||||||
"checksum adler32 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7e522997b529f05601e05166c07ed17789691f562762c7f3b987263d2dedee5c"
|
"checksum adler32 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7e522997b529f05601e05166c07ed17789691f562762c7f3b987263d2dedee5c"
|
||||||
"checksum aho-corasick 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e6f484ae0c99fec2e858eb6134949117399f222608d84cadb3f58c1f97c2364c"
|
"checksum aho-corasick 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e6f484ae0c99fec2e858eb6134949117399f222608d84cadb3f58c1f97c2364c"
|
||||||
"checksum arc-swap 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)" = "bc4662175ead9cd84451d5c35070517777949a2ed84551764129cedb88384841"
|
"checksum arc-swap 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)" = "bc4662175ead9cd84451d5c35070517777949a2ed84551764129cedb88384841"
|
||||||
|
@ -2317,6 +2365,7 @@ dependencies = [
|
||||||
"checksum core-foundation-sys 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e7ca8a5221364ef15ce201e8ed2f609fc312682a8f4e0e3d4aa5879764e0fa3b"
|
"checksum core-foundation-sys 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e7ca8a5221364ef15ce201e8ed2f609fc312682a8f4e0e3d4aa5879764e0fa3b"
|
||||||
"checksum crc 1.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d663548de7f5cca343f1e0a48d14dcfb0e9eb4e079ec58883b7251539fa10aeb"
|
"checksum crc 1.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d663548de7f5cca343f1e0a48d14dcfb0e9eb4e079ec58883b7251539fa10aeb"
|
||||||
"checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1"
|
"checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1"
|
||||||
|
"checksum crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "0f0ed1a4de2235cabda8558ff5840bffb97fcb64c97827f354a451307df5f72b"
|
||||||
"checksum crossbeam-deque 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b18cd2e169ad86297e6bc0ad9aa679aee9daa4f19e8163860faf7c164e4f5a71"
|
"checksum crossbeam-deque 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b18cd2e169ad86297e6bc0ad9aa679aee9daa4f19e8163860faf7c164e4f5a71"
|
||||||
"checksum crossbeam-epoch 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "04c9e3102cc2d69cd681412141b390abd55a362afc1540965dad0ad4d34280b4"
|
"checksum crossbeam-epoch 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "04c9e3102cc2d69cd681412141b390abd55a362afc1540965dad0ad4d34280b4"
|
||||||
"checksum crossbeam-queue 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7c979cd6cfe72335896575c6b5688da489e420d36a27a0b9eb0c73db574b4a4b"
|
"checksum crossbeam-queue 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7c979cd6cfe72335896575c6b5688da489e420d36a27a0b9eb0c73db574b4a4b"
|
||||||
|
|
|
@ -5,10 +5,12 @@ authors = ["Edward Shen <code@eddie.sh>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
reqwest = "0.9.15"
|
reqwest = "0.9"
|
||||||
serde = { version = "1.0.90", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
ron = "0.5"
|
ron = "0.5"
|
||||||
actix-web = "1.0.0-beta.2"
|
actix-web = "1.0.0-beta.2"
|
||||||
|
actix = "0.8"
|
||||||
tokio = "0.1"
|
tokio = "0.1"
|
||||||
tera = "0.11"
|
tera = "0.11"
|
||||||
env_logger = "0.6.1"
|
env_logger = "0.6"
|
||||||
|
lazy_static = "1.3.0"
|
||||||
|
|
|
@ -16,7 +16,7 @@ pub struct WebsiteConfig {
|
||||||
pub endpoints: Vec<EndpointConfig>,
|
pub endpoints: Vec<EndpointConfig>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
#[derive(Deserialize, Serialize, Debug, Clone, Default)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub refresh_time: u64,
|
pub refresh_time: u64,
|
||||||
pub bind_address: String,
|
pub bind_address: String,
|
||||||
|
|
142
src/handlers.rs
Normal file
142
src/handlers.rs
Normal file
|
@ -0,0 +1,142 @@
|
||||||
|
use crate::config::*;
|
||||||
|
use crate::utils::EpochTimestamp;
|
||||||
|
use actix_web::{
|
||||||
|
error::ErrorInternalServerError, web::Data, Error as WebError, HttpResponse,
|
||||||
|
Result as WebResult,
|
||||||
|
};
|
||||||
|
use reqwest::{Client, Url, UrlError};
|
||||||
|
|
||||||
|
use serde::Serialize;
|
||||||
|
use std::sync::{Arc, Mutex, MutexGuard};
|
||||||
|
use tera::{Context, Tera};
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Default)]
|
||||||
|
pub struct Status {
|
||||||
|
status: u8,
|
||||||
|
location: String,
|
||||||
|
domain: String,
|
||||||
|
endpoint: String,
|
||||||
|
error: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct QueryResults {
|
||||||
|
pub last_update: EpochTimestamp,
|
||||||
|
pub refresh_time: u64,
|
||||||
|
pub config: Config,
|
||||||
|
pub statuses: Vec<Status>,
|
||||||
|
}
|
||||||
|
|
||||||
|
type State = Arc<Mutex<QueryResults>>;
|
||||||
|
|
||||||
|
// impl State {
|
||||||
|
// pub fn new(config: &Config) -> Self {
|
||||||
|
// State(Arc::from(Mutex::from(QueryResults {
|
||||||
|
// last_update: EpochTimestamp::now(),
|
||||||
|
// refresh_time: config.refresh_time.clone(),
|
||||||
|
// config: config.clone(),
|
||||||
|
// statuses: vec![],
|
||||||
|
// })))
|
||||||
|
// }
|
||||||
|
|
||||||
|
// fn lock(&self) -> MutexGuard<QueryResults> {
|
||||||
|
// self.0.lock().unwrap()
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
pub fn index(tmpl: Data<Tera>, state: Data<State>) -> WebResult<HttpResponse, WebError> {
|
||||||
|
let state = update_state(state.lock().unwrap());
|
||||||
|
let mut ctx = Context::new();
|
||||||
|
ctx.insert("results", &*state);
|
||||||
|
let s = tmpl.render("index.html", &ctx).map_err(|e| {
|
||||||
|
println!("{:?}", e);
|
||||||
|
ErrorInternalServerError("Template error")
|
||||||
|
})?;
|
||||||
|
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn json_endpoint(state: Data<State>) -> HttpResponse {
|
||||||
|
let state = update_state(state.lock().unwrap());
|
||||||
|
HttpResponse::Ok().json(&state.statuses)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_state(mut state: MutexGuard<QueryResults>) -> MutexGuard<QueryResults> {
|
||||||
|
if EpochTimestamp::now() - state.last_update >= state.refresh_time {
|
||||||
|
state.last_update = EpochTimestamp::now();
|
||||||
|
state.statuses = update_status(&state.config);
|
||||||
|
}
|
||||||
|
|
||||||
|
state
|
||||||
|
}
|
||||||
|
|
||||||
|
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())
|
||||||
|
}
|
165
src/main.rs
165
src/main.rs
|
@ -1,172 +1,69 @@
|
||||||
|
extern crate actix;
|
||||||
extern crate actix_web;
|
extern crate actix_web;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
extern crate reqwest;
|
extern crate reqwest;
|
||||||
extern crate ron;
|
extern crate ron;
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
|
extern crate tokio;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate tera;
|
extern crate tera;
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
|
mod handlers;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
use self::config::*;
|
use self::config::*;
|
||||||
|
use self::handlers::*;
|
||||||
use self::utils::EpochTimestamp;
|
use self::utils::EpochTimestamp;
|
||||||
use actix_web::{
|
use actix::System;
|
||||||
error::ErrorInternalServerError,
|
use actix_web::{middleware::Logger, web::resource, App, HttpServer};
|
||||||
middleware::Logger,
|
|
||||||
web::{resource, Data},
|
|
||||||
App, Error as WebError, HttpResponse, HttpServer, Result as WebResult,
|
|
||||||
};
|
|
||||||
use reqwest::{Client, Url, UrlError};
|
|
||||||
use ron::de::from_str;
|
use ron::de::from_str;
|
||||||
use serde::Serialize;
|
|
||||||
use std::{
|
use std::{
|
||||||
error::Error,
|
error::Error,
|
||||||
fs::read_to_string,
|
fs::read_to_string,
|
||||||
sync::{Arc, Mutex, MutexGuard},
|
sync::{Arc, Mutex},
|
||||||
|
time::Duration,
|
||||||
};
|
};
|
||||||
use tera::{Context, Tera};
|
use tokio::prelude::{Future, Stream};
|
||||||
|
use tokio::timer::Interval;
|
||||||
|
|
||||||
#[derive(Clone, Serialize)]
|
fn main() {
|
||||||
pub struct Status {
|
System::run(move || {
|
||||||
status: u8,
|
let config = from_str::<Config>(&read_to_string("./endstat_conf.ron").expect("Could not find config file")).unwrap();
|
||||||
location: String,
|
|
||||||
domain: String,
|
|
||||||
endpoint: String,
|
|
||||||
error: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
pub struct FetchResults {
|
|
||||||
last_update: EpochTimestamp,
|
|
||||||
refresh_time: u64,
|
|
||||||
config: Config,
|
|
||||||
statuses: Vec<Status>,
|
|
||||||
}
|
|
||||||
|
|
||||||
type StatusState = Arc<Mutex<FetchResults>>;
|
|
||||||
|
|
||||||
fn index(tmpl: Data<Tera>, state: Data<StatusState>) -> WebResult<HttpResponse, WebError> {
|
|
||||||
let state = update_state(state.lock().unwrap());
|
|
||||||
let mut ctx = Context::new();
|
|
||||||
ctx.insert("results", &*state);
|
|
||||||
let s = tmpl.render("index.html", &ctx).map_err(|e| {
|
|
||||||
println!("{:?}", e);
|
|
||||||
ErrorInternalServerError("Template error")
|
|
||||||
})?;
|
|
||||||
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn json_endpoint(state: Data<StatusState>) -> HttpResponse {
|
|
||||||
let state = update_state(state.lock().unwrap());
|
|
||||||
HttpResponse::Ok().json(&state.statuses)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update_state(mut state: MutexGuard<FetchResults>) -> MutexGuard<FetchResults> {
|
|
||||||
if EpochTimestamp::now() - state.last_update >= state.refresh_time {
|
|
||||||
state.last_update = EpochTimestamp::now();
|
|
||||||
state.statuses = update_status(&state.config);
|
|
||||||
}
|
|
||||||
|
|
||||||
state
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() -> Result<(), Box<Error>> {
|
|
||||||
let config = from_str::<Config>(&read_to_string("./endstat_conf.ron")?)?;
|
|
||||||
let bind_addr = config.bind_address.clone();
|
let bind_addr = config.bind_address.clone();
|
||||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
HttpServer::new(move || {
|
let state = Arc::from(Mutex::from(QueryResults {
|
||||||
let state = Arc::from(Mutex::from(FetchResults {
|
|
||||||
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(),
|
||||||
statuses: update_status(&config),
|
statuses: vec![],
|
||||||
}));
|
}));
|
||||||
|
let state1 = state.clone();
|
||||||
|
let state2 = state.clone();
|
||||||
|
|
||||||
|
HttpServer::new(move || {
|
||||||
let tera = compile_templates!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*"));
|
let tera = compile_templates!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*"));
|
||||||
|
|
||||||
App::new()
|
App::new()
|
||||||
.data(state)
|
.data(state1)
|
||||||
.data(tera)
|
.data(tera)
|
||||||
.wrap(Logger::default())
|
.wrap(Logger::default())
|
||||||
.service(resource("/").to(index))
|
.service(resource("/").to(index))
|
||||||
.service(resource("/api").to(json_endpoint))
|
.service(resource("/api").to(json_endpoint))
|
||||||
})
|
})
|
||||||
.bind(&bind_addr)?
|
.bind(&bind_addr)
|
||||||
.run()?;
|
.expect("Could not bind to address!")
|
||||||
|
.start();
|
||||||
|
|
||||||
|
tokio::spawn(
|
||||||
|
Interval::new_interval(Duration::from_millis(5000))
|
||||||
|
.for_each(|_| {
|
||||||
|
println!("Every 5 seconds");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
})
|
||||||
|
.map_err(|_| ()),
|
||||||
fn update_status(config: &Config) -> Vec<Status> {
|
);
|
||||||
let client = Client::new();
|
}).expect("Could not run system!");
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,7 @@
|
||||||
<body>
|
<body>
|
||||||
<main>
|
<main>
|
||||||
<h1>Welcome!</h1>
|
<h1>Welcome!</h1>
|
||||||
|
<h1>{{ results.last_update }}</h1>
|
||||||
{% for status in results.statuses -%}
|
{% for status in results.statuses -%}
|
||||||
<section>
|
<section>
|
||||||
<p>{{ status.domain }}</p>
|
<p>{{ status.domain }}</p>
|
||||||
|
|
Loading…
Reference in a new issue