aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2022-10-28 13:40:37 +0200
committerMartin Fischer <martin@push-f.com>2022-10-28 13:41:18 +0200
commit6d2b42da54335682bf5ba25277323daafca16808 (patch)
tree4cec078db09052cbf9b9a78bc9e2a036ccf4c02a /src
parent025d2619341a6323d87c2cfa3dcf0a0f287c6636 (diff)
improve error page rendering
Previously 404 pages did not set the viewport meta tag, which made the links very hard to click on mobile.
Diffstat (limited to 'src')
-rw-r--r--src/error.rs12
-rw-r--r--src/main.rs32
2 files changed, 26 insertions, 18 deletions
diff --git a/src/error.rs b/src/error.rs
index 08829a5..a49301a 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -5,7 +5,7 @@ use sputnik::request::QueryError;
use sputnik::response::EmptyBuilder;
use std::str::Utf8Error;
-use crate::HyperResponse;
+use crate::{HyperResponse, Page};
/// For convenience this enum also contains nonerroneous variants.
#[derive(Debug)]
@@ -84,11 +84,17 @@ impl From<Error> for HyperResponse {
.unwrap();
}
};
- // TODO: use Page
Builder::new()
.status(status)
.header("content-type", "text/html")
- .body(message.into())
+ .body(
+ Page {
+ body: format!("<div>{}</div>", message),
+ ..Page::default()
+ }
+ .render()
+ .into(),
+ )
.unwrap()
}
}
diff --git a/src/main.rs b/src/main.rs
index af00d3f..e82cfae 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -234,7 +234,7 @@ async fn service<C: Controller>(
);
Builder::new()
.content_type(mime::TEXT_HTML)
- .body(render_page(&page).into())
+ .body(page.render().into())
.unwrap()
}
})
@@ -272,21 +272,23 @@ pub struct Page {
frame_src: Option<&'static str>,
}
-fn render_page(page: &Page) -> String {
- let mut out = String::new();
- out.push_str("<!doctype html><html><head><meta charset=utf-8>");
- out.push_str(&format!("<title>{}</title>", html_escape(&page.title)));
- out.push_str("<meta name=viewport content=\"width=device-width, initial-scale=1\"><style>");
- out.push_str(include_str!("static/style.css"));
- out.push_str("</style></head><body><header id=header>");
- out.push_str(&page.header);
- out.push_str("</header>");
- out.push_str(&page.body);
- for script in &page.scripts {
- out.push_str(&format!("<script>{}</script>", script));
+impl Page {
+ fn render(&self) -> String {
+ let mut out = String::new();
+ out.push_str("<!doctype html><html><head><meta charset=utf-8>");
+ out.push_str(&format!("<title>{}</title>", html_escape(&self.title)));
+ out.push_str("<meta name=viewport content=\"width=device-width, initial-scale=1\"><style>");
+ out.push_str(include_str!("static/style.css"));
+ out.push_str("</style></head><body><header id=header>");
+ out.push_str(&self.header);
+ out.push_str("</header>");
+ out.push_str(&self.body);
+ for script in &self.scripts {
+ out.push_str(&format!("<script>{}</script>", script));
+ }
+ out.push_str("</body></html>");
+ out
}
- out.push_str("</body></html>");
- out
}
#[derive(Deserialize)]