use crate::{config::*, State}; use actix_web::{ error::ErrorInternalServerError, web::Data, Error as WebError, HttpResponse, Result as WebResult, }; use chrono::prelude::*; use serde::Serialize; use tera::{Context, Tera}; #[derive(Clone, Serialize, Default, Debug)] pub struct EndpointStatus { pub status: u8, pub location: String, pub endpoint: String, pub error: Option, } impl EndpointStatus { pub fn ok(location: String, endpoint: String) -> Self { EndpointStatus { status: 0, location, endpoint, error: None, } } pub fn warn(location: String, endpoint: String, error: Option) -> Self { EndpointStatus { status: 1, location, endpoint, error, } } pub fn error(location: String, endpoint: String, error: Option) -> Self { EndpointStatus { status: 2, location, endpoint, error, } } } #[derive(Serialize, Debug)] pub struct StatusGroup { pub label: String, pub endpoints: Vec, } #[derive(Serialize, Debug)] pub struct QueryResults { pub timestamp: DateTime, pub timestamp_str: String, pub refresh_time: u64, pub config: Config, pub groups: Vec, } pub fn index(tmpl: Data, state: Data) -> WebResult { let state = state.read().unwrap(); 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)) } pub fn json_endpoint(state: Data) -> HttpResponse { let state = state.read().unwrap(); HttpResponse::Ok().json(&*state.groups) }