use systemd::unit; use horrorshow::prelude::*; use horrorshow::Raw; #[derive(Debug)] pub struct Renderer { pub base_url: String, pub user: Option, } impl Renderer { fn get_url(&self, path: &str) -> String { format!("{}{}", self.base_url, path) } fn render_in_page<'a>(&self, content: Box) -> String { info!("Rendering page with context: {:?}", self); let login_box: Box = match self.user { Some(ref user) => { box_html!{ : "Logged in as "; : user; : " ("; a(href=self.get_url("logout")) { : "log out" } : ")"; } } None => { box_html! { a(href=self.get_url("login")) { // TODO Get base url from context : "Log in" } } } }; (html!{ : Raw(""); html { head { title: "Systemhttpd"; link(rel="stylesheet", type="text/css", href=self.get_url("static/main.css")); } body { nav { ul { li { p { : "SystemHttpd" } } } ul(class="right") { li { p { : login_box } } } } main { : content } } } }) .into_string() .unwrap() } pub fn login_page(&self, is_retry: bool) -> String { self.render_in_page(box_html! { h1 { : "Log in" } @ if is_retry { p { : "Incorrect username or password. Try again." } } form(method="post") { p { : "Username" } input(type="text", name="username") {} p { : "Password" } input(type="text", name="password") {} p {} input(type="submit", value="Log in") {} } }) } fn render_unit<'a>(&'a self, unit: &'a unit::Unit) -> Box { box_html! { tr { td { a(href=self.get_url(&format!("/status/{}", &unit.name))) { : &unit.name } } td { : format_args!("{} ({})", &unit.active_state, &unit.sub_state) } } } } fn unit_table<'a>(&'a self, units: &'a [&unit::Unit]) -> Box { box_html! { table { tr { th { : "Unit" } th { : "Active" } } @ for unit in units { : self.render_unit(unit) } } } } pub fn system_status(&self, sections: &[(String, Vec<&unit::Unit>)]) -> String { self.render_in_page(box_html! { @ for &(ref type_, ref units) in sections { h1 { : type_ } : self.unit_table(&units) } }) } pub fn unit_status(&self, unit: &unit::Unit, log: &str) -> String { self.render_in_page(box_html! { h1 { :&unit.name } p { : format_args!("{} ({})", &unit.active_state, &unit.sub_state) } pre { : log } }) } }