use crate::State; use actix_web::{ error::ErrorInternalServerError, web::Data, Error as WebError, HttpResponse, Result as WebResult, }; use tera::{Context, Tera}; #[cfg(feature = "rss_feed")] use rss::{ChannelBuilder, ItemBuilder}; 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)) } #[cfg(feature = "json")] pub fn json_endpoint(state: Data) -> HttpResponse { let state = state.read().unwrap(); HttpResponse::Ok().json(&*state.groups) } #[cfg(feature = "rss_feed")] pub fn rss_endpoint(state: Data) -> HttpResponse { let state = state.read().unwrap(); let mut chan = ChannelBuilder::default() .title("endstat") .description("Endpoint statuses") .link("https://git.eddie.sh/edward/endstat") .build() .unwrap(); let mut endpoints = vec![]; for group in &state.groups { for endpoint in &group.endpoints { endpoints.push( ItemBuilder::default() .title(format!("{}: {}", group.label, endpoint.label)) .link(endpoint.location.clone()) .description(Some(endpoint.status.clone().into())) .build() .unwrap() ); } } chan.set_items(endpoints); HttpResponse::Ok().content_type("application/rss+xml").body(chan.to_string()) }