added basic readme, api endpoint

master
Edward Shen 2019-04-30 04:20:01 -04:00
parent b1d73e075b
commit 88e9c4df82
Signed by: edward
GPG Key ID: F350507060ED6C90
2 changed files with 22 additions and 4 deletions

6
README.md Normal file
View File

@ -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.

View File

@ -7,11 +7,11 @@ extern crate serde;
use actix::System; use actix::System;
use actix_web::{ use actix_web::{
http::{Method, StatusCode}, http::{Method, StatusCode},
server, App, HttpResponse, State, server, App, HttpResponse, Json, Result as WebResult, State,
}; };
use reqwest::{Client, Url, UrlError}; use reqwest::{Client, Url, UrlError};
use ron::de::from_str; use ron::de::from_str;
use serde::Deserialize; use serde::{Deserialize, Serialize};
use std::{ use std::{
error::Error, error::Error,
fs::read_to_string, fs::read_to_string,
@ -41,7 +41,7 @@ struct Config {
websites: Vec<WebsiteConf>, websites: Vec<WebsiteConf>,
} }
#[derive(Debug)] #[derive(Debug, Clone, Serialize)]
struct Status { struct Status {
status: u8, status: u8,
location: String, location: String,
@ -75,6 +75,16 @@ fn index(state: State<StatusState>) -> HttpResponse {
HttpResponse::with_body(StatusCode::OK, result) HttpResponse::with_body(StatusCode::OK, result)
} }
fn json_endpoint(state: State<StatusState>) -> WebResult<Json<Vec<Status>>> {
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<Error>> { fn main() -> Result<(), Box<Error>> {
let config = from_str::<Config>(&read_to_string("./endstat_conf.ron")?)?; let config = from_str::<Config>(&read_to_string("./endstat_conf.ron")?)?;
@ -89,7 +99,9 @@ fn main() -> Result<(), Box<Error>> {
})); }));
server::new(move || { 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")? .bind("0.0.0.0:8080")?
.start(); .start();