Add sitemap. This fixes #3

This commit is contained in:
Magnus Hoff 2017-10-13 15:06:16 +02:00
parent 5f93f3d744
commit 2cb80e2bac
8 changed files with 126 additions and 6 deletions

View file

@ -170,21 +170,21 @@ footer {
"Droid Sans", "Helvetica Neue", sans-serif;
}
footer ul {
ul.dense {
list-style: none;
padding: 0;
}
footer li {
ul.dense>li {
display: inline;
}
footer li::after {
ul.dense>li::after {
content: "\00B7";
margin: 0 12px;
}
footer li:last-child::after {
ul.dense>li:last-child::after {
content: "";
margin: 0;
}

View file

@ -34,6 +34,7 @@ mod new_article_resource;
mod rendering;
mod schema;
mod site;
mod sitemap_resource;
mod state;
mod web;
mod wiki_lookup;

69
src/sitemap_resource.rs Normal file
View file

@ -0,0 +1,69 @@
use futures::{self, Future};
use hyper;
use hyper::header::ContentType;
use hyper::server::*;
use assets::StyleCss;
use mimes::*;
use site::Layout;
use state::State;
use web::{Resource, ResponseFuture};
pub struct SitemapResource {
state: State,
}
impl SitemapResource {
pub fn new(state: State) -> Self {
SitemapResource { state }
}
}
impl Resource for SitemapResource {
fn allow(&self) -> Vec<hyper::Method> {
use hyper::Method::*;
vec![Options, Head, Get]
}
fn head(&self) -> ResponseFuture {
Box::new(futures::finished(Response::new()
.with_status(hyper::StatusCode::Ok)
.with_header(ContentType(TEXT_HTML.clone()))
))
}
fn get(self: Box<Self>) -> ResponseFuture {
#[derive(BartDisplay)]
#[template="templates/sitemap.html"]
struct Template<'a> {
articles: &'a [ArticleReference],
}
struct ArticleReference {
link: String,
title: String,
}
let data = self.state.get_latest_article_revision_stubs();
let head = self.head();
Box::new(data.join(head)
.and_then(move |(articles, head)| {
use std::iter::Iterator;
let articles = &articles.into_iter().map(|x| {
ArticleReference {
link: if x.slug.is_empty() { ".".to_owned() } else { x.slug },
title: x.title,
}
}).collect::<Vec<_>>();
Ok(head
.with_body(Layout {
title: "Sitemap",
body: &Template { articles },
style_css_checksum: StyleCss::checksum(),
}.to_string()))
}))
}
}

View file

@ -129,6 +129,28 @@ impl State {
})
}
pub fn get_latest_article_revision_stubs(&self) -> CpuFuture<Vec<models::ArticleRevisionStub>, Error> {
let connection_pool = self.connection_pool.clone();
self.cpu_pool.spawn_fn(move || {
use schema::article_revisions;
Ok(article_revisions::table
.filter(article_revisions::latest.eq(true))
.order(article_revisions::title.asc())
.select((
article_revisions::sequence_number,
article_revisions::article_id,
article_revisions::revision,
article_revisions::created,
article_revisions::slug,
article_revisions::title,
article_revisions::latest,
))
.load(&*connection_pool.get()?)?)
})
}
pub fn lookup_slug(&self, slug: String) -> CpuFuture<SlugLookup, Error> {
#[derive(Queryable)]
struct ArticleRevisionStub {

View file

@ -7,6 +7,7 @@ use article_redirect_resource::ArticleRedirectResource;
use article_resource::ArticleResource;
use assets::*;
use new_article_resource::NewArticleResource;
use sitemap_resource::SitemapResource;
use state::State;
use web::{Lookup, Resource};
@ -27,6 +28,13 @@ lazy_static! {
) as ResourceFn
);
lookup_map.insert(
"/_sitemap".to_string(),
Box::new(|state: &State|
Box::new(SitemapResource::new(state.clone())) as BoxResource
) as ResourceFn
);
lookup_map.insert(
"/_new".to_string(),
Box::new(|state: &State|

View file

@ -32,7 +32,7 @@
</div>
<footer>
<ul>
<ul class="dense">
<li><a href="_new">Create article</a></li>
<li><a id="openEditor" href="?edit">Edit</a></li>
</ul>

View file

@ -20,7 +20,7 @@
</div>
<footer>
<ul>
<ul class="dense">
<li><a href="_new">Create article</a></li>
</ul>
<p>Powered by <a href="https://github.com/maghoff/sausagewiki">Sausagewiki</a></p>

20
templates/sitemap.html Normal file
View file

@ -0,0 +1,20 @@
<div class="container">
<header>
<h1>Sitemap</h1>
</header>
<article>
<ul class="dense">
{{#articles}}
<li><a href="{{.link}}">{{.title}}</a></li>
{{/articles}}
</ul>
</article>
</div>
<footer>
<ul class="dense">
<li><a href="_new">Create article</a></li>
</ul>
<p>Powered by <a href="https://github.com/maghoff/sausagewiki">Sausagewiki</a></p>
</footer>