From 16055300c760c636399f555ce30c07deff2a6820 Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Mon, 26 Nov 2018 21:01:09 +0100 Subject: Redo response builder usage - Set correct content-type header - Refactor views to use one base-view --- src/request_handler/views.rs | 179 +++++++++++++++++++------------------------ 1 file changed, 78 insertions(+), 101 deletions(-) (limited to 'src/request_handler/views.rs') diff --git a/src/request_handler/views.rs b/src/request_handler/views.rs index ee020eb..1a239a4 100644 --- a/src/request_handler/views.rs +++ b/src/request_handler/views.rs @@ -1,138 +1,115 @@ -use horrorshow::Template; +use std::boxed::Box; +use horrorshow::{Render, RenderBox, Template}; -pub(in super) fn info_debug<'a>(path_rest: &'a str, cookies: Vec<(String, String)>) -> String { + +fn render_base_template(title: &'static str, page_body: Box) -> String { (html! { : horrorshow::helper::doctype::HTML; html { head { - title: "Hello world!"; + title: title; + meta(name="viewport", content="width=device-width, initial-scale=1.5"); } body { - h1(id = "heading") { - : "Hello! This is "; - : "And path rest is: "; - : path_rest; - : "... ok :)"; - } - h2: "Valid cookies are:"; - table(border="1") { - thead { - th: "Cookie value"; - th: "Valid until"; - } - tbody { - @ for (name, valid_until) in cookies { - tr { - td: name; - td: valid_until; - } - } - } - } + : page_body; } } }).into_string().unwrap() } -pub(in super) fn info<'a>(path_rest: &'a str) -> String { - (html! { - : horrorshow::helper::doctype::HTML; - html { - head { - title: "Hello world!"; +pub(in super) fn info_debug<'a>(path_rest: &'a str, cookies: Vec<(String, String)>) -> String { + let path = path_rest.to_string(); + render_base_template("Info (debug)", box_html! { + h1(id = "heading") { + : "Hello! This is "; + : "And path rest is: "; + : path; + : "... ok :)"; + } + h2: "Valid cookies are:"; + table(border="1") { + thead { + th: "Cookie value"; + th: "Valid until"; } - body { - h1(id = "heading") { - : "Hello! This is "; - : "And path rest is: "; - : path_rest; - : "... ok :)"; + tbody { + @ for (name, valid_until) in cookies { + tr { + td: name; + td: valid_until; + } } } } - }).into_string().unwrap() + }) +} + +pub(in super) fn info<'a>(path_rest: &'a str) -> String { + let path = path_rest.to_string(); + render_base_template("Info", box_html! { + h1(id = "heading") { + : "Hello! This is "; + : "And path rest is: "; + : path; + : "... ok :)"; + } + }) } pub(in super) fn login_is_logged_in() -> String { - (html! { - : horrorshow::helper::doctype::HTML; - html { - head { - title: "TOTP Login"; - } - body { - h1(id = "heading") { - : "Currently logged in" - } - } + render_base_template("Logged in", box_html! { + h1(id = "heading") { + : "Currently logged in" } - }).into_string().unwrap() + }) } pub(in super) fn login_login_form<'a>(redirect: &'a str) -> String { - (html! { - : horrorshow::helper::doctype::HTML; - html { - head { - title: "TOTP Login"; - } - body { - h1(id = "heading") { - : "Login" - } - form(method="POST") { - label(for="token") { + let redirect = redirect.to_string(); + render_base_template("TOTP Login", box_html! { + h1(id = "heading") { + : "Login" + } + form(method="POST") { + div { + label(for="token") { : "Enter TOTP token" - } - input(name="token",id="token",type="text"); - input(name="redirect", type="hidden", value=redirect); - input(name="send",type="submit",value="Submit"); } } + div { + input(name="token",id="token",type="number",autocomplete="off",required=""); + input(name="redirect", type="hidden", value=redirect); + } + div { + input(name="send",type="submit",value="Submit"); + } } - }).into_string().unwrap() + }) } pub(in super) fn login_auth_success(redirect: &String) -> String { - (html! { - : horrorshow::helper::doctype::HTML; - html { - head { - title: "TOTP Successful"; - meta(http-equiv="refresh", content=format!("3; URL={}", redirect)) - } - body { - h1(id = "heading") { - : "Login succesful" - } - a(href=redirect) { - : "Try again... redirecting to "; - } - span { - : format!("{}", redirect) - } - } + let redirect = redirect.clone(); + render_base_template("Login successful", box_html! { + h1(id = "heading") { + : "Login succesful" } - }).into_string().unwrap() + a(href=&redirect) { + : "redirecting to "; + } + span { + : format!("{}", redirect) + } + }) } pub(in super) fn login_auth_fail() -> String { - (html! { - : horrorshow::helper::doctype::HTML; - html { - head { - title: "TOTP Login failed"; - meta(http-equiv="refresh", content="1") - } - body { - h1(id = "heading") { - : "Login failed" - } - a(href="login") { - : "Try again... " - } - } + render_base_template("Login failed", box_html! { + h1(id = "heading") { + : "Login failed" } - }).into_string().unwrap() + a(href="login") { + : "Try again... " + } + }) } \ No newline at end of file -- cgit v1.2.1