endstat/src/handlers.rs

23 lines
726 B
Rust

use crate::State;
use actix_web::{
error::ErrorInternalServerError, web::Data, Error as WebError, HttpResponse,
Result as WebResult,
};
use tera::{Context, Tera};
pub fn index(tmpl: Data<Tera>, state: Data<State>) -> WebResult<HttpResponse, WebError> {
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<State>) -> HttpResponse {
let state = state.read().unwrap();
HttpResponse::Ok().json(&*state.groups)
}