diff --git a/README.md b/README.md new file mode 100644 index 0000000..947a11b --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# endstat + +EndStat is an easy-to-use lazy **End**point **Stat**us checking tool, meant for +checking the health of various web locations. It supports arbitrary domains and +ports, status matching, and body matching using a quick-to-understand config +file. diff --git a/src/main.rs b/src/main.rs index 40d5621..feca341 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,11 +7,11 @@ extern crate serde; use actix::System; use actix_web::{ http::{Method, StatusCode}, - server, App, HttpResponse, State, + server, App, HttpResponse, Json, Result as WebResult, State, }; use reqwest::{Client, Url, UrlError}; use ron::de::from_str; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use std::{ error::Error, fs::read_to_string, @@ -41,7 +41,7 @@ struct Config { websites: Vec, } -#[derive(Debug)] +#[derive(Debug, Clone, Serialize)] struct Status { status: u8, location: String, @@ -75,6 +75,16 @@ fn index(state: State) -> HttpResponse { HttpResponse::with_body(StatusCode::OK, result) } +fn json_endpoint(state: State) -> WebResult>> { + let mut state = state.lock().unwrap(); + if Instant::now().duration_since(state.last_update) > Duration::from_secs(state.refresh_time) { + state.last_update = Instant::now(); + state.statuses = update_status(&state.config); + } + + Ok(Json(state.statuses.clone())) +} + fn main() -> Result<(), Box> { let config = from_str::(&read_to_string("./endstat_conf.ron")?)?; @@ -89,7 +99,9 @@ fn main() -> Result<(), Box> { })); server::new(move || { - App::with_state(a.clone()).resource("/", |r| r.method(Method::GET).with(index)) + App::with_state(a.clone()) + .resource("/", |r| r.get().with(index)) + .resource("/api", |r| r.get().with(json_endpoint)) }) .bind("0.0.0.0:8080")? .start();