Compare commits
2 commits
cb9e094582
...
1428959602
Author | SHA1 | Date | |
---|---|---|---|
1428959602 | |||
9a9d87038a |
3 changed files with 142 additions and 84 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)
|
||||
}
|
132
src/main.rs
132
src/main.rs
|
@ -6,8 +6,10 @@ extern crate serde;
|
|||
#[macro_use]
|
||||
extern crate tera;
|
||||
|
||||
mod config;
|
||||
mod utils;
|
||||
|
||||
use self::config::*;
|
||||
use self::utils::EpochTimestamp;
|
||||
use actix_web::{
|
||||
error::ErrorInternalServerError,
|
||||
|
@ -17,7 +19,7 @@ use actix_web::{
|
|||
};
|
||||
use reqwest::{Client, Url, UrlError};
|
||||
use ron::de::from_str;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde::Serialize;
|
||||
use std::{
|
||||
error::Error,
|
||||
fs::read_to_string,
|
||||
|
@ -25,29 +27,6 @@ use std::{
|
|||
};
|
||||
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)]
|
||||
pub struct Status {
|
||||
status: u8,
|
||||
|
@ -71,9 +50,10 @@ fn index(tmpl: Data<Tera>, state: Data<StatusState>) -> WebResult<HttpResponse,
|
|||
let state = update_state(state.lock().unwrap());
|
||||
let mut ctx = Context::new();
|
||||
ctx.insert("results", &*state);
|
||||
let s = tmpl
|
||||
.render("index.html", &tera::Context::new())
|
||||
.map_err(|_| ErrorInternalServerError("Template error"))?;
|
||||
let s = tmpl.render("index.html", &ctx).map_err(|e| {
|
||||
println!("{:?}", e);
|
||||
ErrorInternalServerError("Template error")
|
||||
})?;
|
||||
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
||||
}
|
||||
|
||||
|
@ -125,49 +105,64 @@ fn update_status(config: &Config) -> Vec<Status> {
|
|||
|
||||
for website_conf in &config.websites {
|
||||
for endpoint in &website_conf.endpoints {
|
||||
let (label, path, port, code, body) = get_endpoint_info(endpoint.clone());
|
||||
let url = get_url(&website_conf.base, &path, port).expect("reading config");
|
||||
if let Ok(mut res) = client.get(&url).send() {
|
||||
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;
|
||||
|
||||
results.push(if !does_code_match {
|
||||
Status {
|
||||
status: 1,
|
||||
location: url,
|
||||
domain: website_conf.label.clone(),
|
||||
endpoint: label,
|
||||
error: Some(format!(
|
||||
"Status code mismatch! {} != {}",
|
||||
res.status().as_u16(),
|
||||
code
|
||||
)),
|
||||
}
|
||||
} else if !does_body_match {
|
||||
Status {
|
||||
status: 2,
|
||||
location: url,
|
||||
domain: website_conf.label.clone(),
|
||||
endpoint: label,
|
||||
error: Some(format!("Body mismatch! {} != {}", res_body, body)),
|
||||
}
|
||||
} else {
|
||||
Status {
|
||||
status: 0,
|
||||
location: url,
|
||||
domain: website_conf.label.clone(),
|
||||
endpoint: label,
|
||||
error: None,
|
||||
}
|
||||
});
|
||||
}
|
||||
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) {
|
||||
|
@ -175,12 +170,3 @@ fn get_url(base: &String, path: &String, port: Option<u16>) -> Result<String, Ur
|
|||
}
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -1,18 +1,57 @@
|
|||
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<title>Actix web</title>
|
||||
<title>Endstat</title>
|
||||
<style>
|
||||
body {
|
||||
background-color: #212121;
|
||||
margin: 0;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
main {
|
||||
width: 700px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
section {
|
||||
display: flex;
|
||||
background-color: #424242;
|
||||
align-items: center;
|
||||
margin: 1rem 0;
|
||||
padding: 1rem;
|
||||
border-radius: 1rem;
|
||||
}
|
||||
|
||||
p { margin: 0; }
|
||||
|
||||
h1 { display: inline; }
|
||||
|
||||
.indicator {
|
||||
width: 1rem;
|
||||
height: 1rem;
|
||||
border-radius: 1rem;
|
||||
}
|
||||
.ok { background-color: green; }
|
||||
.warn { background-color: yellow; }
|
||||
.error { background-color: red; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<h1>Welcome!</h1>
|
||||
<p>
|
||||
<h3>What is your name?</h3>
|
||||
<form>
|
||||
<input type="text" name="name" /><br/>
|
||||
<p><input type="submit"></p>
|
||||
</form>
|
||||
</p>
|
||||
{% for status in results.statuses -%}
|
||||
<section>
|
||||
<p>{{ status.domain }}</p>
|
||||
<p>{{ status.endpoint }}</p>
|
||||
<p>{{ status.location }}</p>
|
||||
{% if status.error %}<p>{{ status.error }}</p>{% endif %}
|
||||
<div class="indicator {% if status.status == 0 %}ok{% elif status.status == 1 %}warn{% else %}error{% endif %}"></div>
|
||||
|
||||
</section>
|
||||
{% endfor -%}
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Reference in a new issue