From b5bcd66011460bcae145a227b856d2500919ee81 Mon Sep 17 00:00:00 2001 From: Magnus Hoff Date: Fri, 13 Oct 2017 16:05:22 +0200 Subject: [PATCH] Introduce base href --- src/resources/article_resource.rs | 1 + src/resources/changes_resource.rs | 1 + src/resources/new_article_resource.rs | 1 + src/resources/sitemap_resource.rs | 1 + src/site.rs | 24 ++++++++++++++++++++---- templates/layout.html | 1 + 6 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/resources/article_resource.rs b/src/resources/article_resource.rs index bec1ed3..4a49c9c 100644 --- a/src/resources/article_resource.rs +++ b/src/resources/article_resource.rs @@ -65,6 +65,7 @@ impl Resource for ArticleResource { .and_then(move |(data, head)| { Ok(head .with_body(Layout { + base: None, // Hmm, should perhaps accept `base` as argument title: &data.title, body: &Template { article_id: data.article_id, diff --git a/src/resources/changes_resource.rs b/src/resources/changes_resource.rs index af4936f..50a2c5b 100644 --- a/src/resources/changes_resource.rs +++ b/src/resources/changes_resource.rs @@ -73,6 +73,7 @@ impl Resource for ChangesResource { Ok(head .with_body(Layout { + base: None, // Hmm, should perhaps accept `base` as argument title: "Changes", body: &Template { changes }, style_css_checksum: StyleCss::checksum(), diff --git a/src/resources/new_article_resource.rs b/src/resources/new_article_resource.rs index f302b55..c65ded7 100644 --- a/src/resources/new_article_resource.rs +++ b/src/resources/new_article_resource.rs @@ -72,6 +72,7 @@ impl Resource for NewArticleResource { .and_then(move |head| { Ok(head .with_body(Layout { + base: None, // Hmm, should perhaps accept `base` as argument title: &title, body: &Template { article_id: NDASH, diff --git a/src/resources/sitemap_resource.rs b/src/resources/sitemap_resource.rs index 61a8f02..07443a5 100644 --- a/src/resources/sitemap_resource.rs +++ b/src/resources/sitemap_resource.rs @@ -60,6 +60,7 @@ impl Resource for SitemapResource { Ok(head .with_body(Layout { + base: None, // Hmm, should perhaps accept `base` as argument title: "Sitemap", body: &Template { articles }, style_css_checksum: StyleCss::checksum(), diff --git a/src/site.rs b/src/site.rs index 5a3fb1e..6400cfe 100644 --- a/src/site.rs +++ b/src/site.rs @@ -20,6 +20,7 @@ lazy_static! { #[derive(BartDisplay)] #[template = "templates/layout.html"] pub struct Layout<'a, T: 'a + fmt::Display> { + pub base: Option<&'a str>, pub title: &'a str, pub body: &'a T, pub style_css_checksum: &'a str, @@ -42,10 +43,11 @@ impl Site { Site { root } } - fn not_found() -> Response { + fn not_found(base: Option<&str>) -> Response { Response::new() .with_header(ContentType(TEXT_HTML.clone())) .with_body(Layout { + base: base, title: "Not found", body: &NotFound, style_css_checksum: StyleCss::checksum(), @@ -53,12 +55,13 @@ impl Site { .with_status(hyper::StatusCode::NotFound) } - fn internal_server_error(err: Box<::std::error::Error + Send + Sync>) -> Response { + fn internal_server_error(base: Option<&str>, err: Box<::std::error::Error + Send + Sync>) -> Response { eprintln!("Internal Server Error:\n{:#?}", err); Response::new() .with_header(ContentType(TEXT_HTML.clone())) .with_body(Layout { + base, title: "Internal server error", body: &InternalServerError, style_css_checksum: StyleCss::checksum(), @@ -67,6 +70,16 @@ impl Site { } } +fn root_base_from_request_uri(path: &str) -> Option { + assert!(path.starts_with("/")); + let slashes = path[1..].matches('/').count(); + + match slashes { + 0 => None, + n => Some(::std::iter::repeat("../").take(n).collect()) + } +} + impl Service for Site { type Request = Request; type Response = Response; @@ -77,6 +90,9 @@ impl Service for Site { let (method, uri, _http_version, _headers, body) = req.deconstruct(); println!("{} {}", method, uri); + let base = root_base_from_request_uri(uri.path()); + let base2 = base.clone(); // Bah, stupid clone + Box::new(self.root.lookup(uri.path(), uri.query()) .and_then(move |resource| match resource { Some(resource) => { @@ -89,9 +105,9 @@ impl Service for Site { _ => Box::new(futures::finished(resource.method_not_allowed())) } }, - None => Box::new(futures::finished(Self::not_found())) + None => Box::new(futures::finished(Self::not_found(base.as_ref().map(|x| &**x)))) }) - .or_else(|err| Ok(Self::internal_server_error(err))) + .or_else(move |err| Ok(Self::internal_server_error(base2.as_ref().map(|x| &**x), err))) ) } } diff --git a/templates/layout.html b/templates/layout.html index 465e2a6..33a41c7 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -3,6 +3,7 @@ {{title}} +{{#base}}{{/base}}