general ui work

This commit is contained in:
Edward Shen 2019-05-01 00:53:14 -04:00
parent cb9e094582
commit 9a9d87038a
Signed by: edward
GPG key ID: F350507060ED6C90
2 changed files with 110 additions and 51 deletions

View file

@ -71,9 +71,10 @@ fn index(tmpl: Data<Tera>, state: Data<StatusState>) -> WebResult<HttpResponse,
let state = update_state(state.lock().unwrap()); let state = update_state(state.lock().unwrap());
let mut ctx = Context::new(); let mut ctx = Context::new();
ctx.insert("results", &*state); ctx.insert("results", &*state);
let s = tmpl let s = tmpl.render("index.html", &ctx).map_err(|e| {
.render("index.html", &tera::Context::new()) println!("{:?}", e);
.map_err(|_| ErrorInternalServerError("Template error"))?; ErrorInternalServerError("Template error")
})?;
Ok(HttpResponse::Ok().content_type("text/html").body(s)) Ok(HttpResponse::Ok().content_type("text/html").body(s))
} }
@ -125,14 +126,25 @@ fn update_status(config: &Config) -> Vec<Status> {
for website_conf in &config.websites { for website_conf in &config.websites {
for endpoint in &website_conf.endpoints { for endpoint in &website_conf.endpoints {
results.push(get_result(website_conf, &client, endpoint));
}
}
results
}
fn get_result(website_conf: &WebsiteConf, client: &Client, endpoint: &EndpointConf) -> Status {
let (label, path, port, code, body) = get_endpoint_info(endpoint.clone()); let (label, path, port, code, body) = get_endpoint_info(endpoint.clone());
let url = get_url(&website_conf.base, &path, port).expect("reading config"); let url = get_url(&website_conf.base, &path, port).expect("reading config");
if let Ok(mut res) = client.get(&url).send() { 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 res_body = res.text().expect("could not get body of request");
let does_code_match = res.status() == code; let does_code_match = res.status() == code;
let does_body_match = body.is_empty() || res_body == body; let does_body_match = body.is_empty() || res_body == body;
results.push(if !does_code_match { if !does_code_match {
Status { Status {
status: 1, status: 1,
location: url, location: url,
@ -146,11 +158,15 @@ fn update_status(config: &Config) -> Vec<Status> {
} }
} else if !does_body_match { } else if !does_body_match {
Status { Status {
status: 2, status: 1,
location: url, location: url,
domain: website_conf.label.clone(), domain: website_conf.label.clone(),
endpoint: label, endpoint: label,
error: Some(format!("Body mismatch! {} != {}", res_body, body)), error: Some(format!(
"Body mismatch! {} != {}",
res_body.len(),
body.len()
)),
} }
} else { } else {
Status { Status {
@ -160,12 +176,16 @@ fn update_status(config: &Config) -> Vec<Status> {
endpoint: label, endpoint: label,
error: None, error: None,
} }
});
} }
} }
Err(e) => Status {
status: 2,
location: url,
domain: website_conf.label.clone(),
endpoint: label,
error: Some(format!("{}", e)),
},
} }
results
} }
fn get_url(base: &String, path: &String, port: Option<u16>) -> Result<String, UrlError> { fn get_url(base: &String, path: &String, port: Option<u16>) -> Result<String, UrlError> {

View file

@ -1,18 +1,57 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html lang="en">
<head> <head>
<meta charset="utf-8" /> <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;
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> </head>
<body> <body>
<main>
<h1>Welcome!</h1> <h1>Welcome!</h1>
<p> {% for status in results.statuses -%}
<h3>What is your name?</h3> <section>
<form> <p>{{ status.domain }}</p>
<input type="text" name="name" /><br/> <p>{{ status.endpoint }}</p>
<p><input type="submit"></p> <p>{{ status.location }}</p>
</form> {% if status.error %}<p>{{ status.error }}</p>{% endif %}
</p> <div class="indicator {% if status.status == 0 %}ok{% elif status.status == 1 %}warn{% else %}error{% endif %}"></div>
</section>
{% endfor -%}
</main>
</body> </body>
</html> </html>