endstat/src/handlers.rs

23 lines
726 B
Rust
Raw Normal View History

2019-05-03 04:54:43 +00:00
use crate::State;
2019-05-01 05:41:52 +00:00
use actix_web::{
error::ErrorInternalServerError, web::Data, Error as WebError, HttpResponse,
Result as WebResult,
};
use tera::{Context, Tera};
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 20:59:55 +00:00
HttpResponse::Ok().json(&*state.groups)
2019-05-01 05:41:52 +00:00
}