diff --git a/src/main.rs b/src/main.rs index b435566..f761ef7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -71,9 +71,10 @@ fn index(tmpl: Data, state: Data) -> WebResult Vec { 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: &WebsiteConf, client: &Client, endpoint: &EndpointConf) -> 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; + + 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: 1, + location: url, + domain: website_conf.label.clone(), + endpoint: label, + error: Some(format!( + "Body mismatch! {} != {}", + res_body.len(), + body.len() + )), + } + } else { + Status { + status: 0, + location: url, + domain: website_conf.label.clone(), + endpoint: label, + error: None, + } + } + } + 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) -> Result { let mut url = Url::parse(base)?.join(path)?; if let Err(e) = url.set_port(port) { diff --git a/templates/index.html b/templates/index.html index 5474bfa..410d0eb 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,18 +1,57 @@ - + - - Actix web + + Endstat + -

Welcome!

-

-

What is your name?

-
-
-

-
-

+
+

Welcome!

+ {% for status in results.statuses -%} +
+

{{ status.domain }}

+

{{ status.endpoint }}

+

{{ status.location }}

+ {% if status.error %}

{{ status.error }}

{% endif %} +
+ +
+ {% endfor -%} +