endstat/src/handlers.rs

43 lines
1.2 KiB
Rust
Raw Normal View History

2019-05-01 20:01:27 +00:00
use crate::{config::*, State};
2019-05-01 05:41:52 +00:00
use actix_web::{
error::ErrorInternalServerError, web::Data, Error as WebError, HttpResponse,
Result as WebResult,
};
2019-05-01 20:01:27 +00:00
use chrono::prelude::*;
2019-05-01 05:41:52 +00:00
use serde::Serialize;
use tera::{Context, Tera};
2019-05-01 16:58:10 +00:00
#[derive(Clone, Serialize, Default, Debug)]
2019-05-01 05:41:52 +00:00
pub struct Status {
2019-05-01 18:13:46 +00:00
pub status: u8,
pub location: String,
pub domain: String,
pub endpoint: String,
pub error: Option<String>,
2019-05-01 05:41:52 +00:00
}
2019-05-01 16:58:10 +00:00
#[derive(Serialize, Debug)]
2019-05-01 06:15:20 +00:00
pub struct QueryResults {
2019-05-01 20:15:18 +00:00
pub timestamp: DateTime<Utc>,
pub timestamp_str: String,
2019-05-01 05:41:52 +00:00
pub refresh_time: u64,
pub config: Config,
pub statuses: Vec<Status>,
}
2019-05-01 06:15:20 +00:00
pub fn index(tmpl: Data<Tera>, state: Data<State>) -> WebResult<HttpResponse, WebError> {
2019-05-01 16:58:10 +00:00
let state = state.read().unwrap();
2019-05-01 05:41:52 +00:00
let mut ctx = Context::new();
ctx.insert("results", &*state);
let s = tmpl.render("index.html", &ctx).map_err(|e| {
println!("{:?}", e);
ErrorInternalServerError("Template error")
})?;
Ok(HttpResponse::Ok().content_type("text/html").body(s))
}
2019-05-01 06:15:20 +00:00
pub fn json_endpoint(state: Data<State>) -> HttpResponse {
2019-05-01 16:58:10 +00:00
let state = state.read().unwrap();
2019-05-01 05:41:52 +00:00
HttpResponse::Ok().json(&state.statuses)
}