Refactor to reuse TemporaryRedirectResource instead of ArticleRedirectResource

This commit is contained in:
Magnus Hoff 2017-10-30 12:56:28 +01:00
parent 1bdc21039b
commit 975661048e
4 changed files with 13 additions and 55 deletions

View file

@ -1,51 +0,0 @@
use futures::{self, Future};
use hyper;
use hyper::header::Location;
use hyper::server::*;
use web::{Resource, ResponseFuture};
pub struct ArticleRedirectResource {
slug: String,
}
impl ArticleRedirectResource {
pub fn new(slug: String) -> Self {
// Hack to let redirects to "" work:
// TODO Calculate absolute Location URLs to conform to spec
// This would also remove the need for this hack
Self {
slug: if slug == "" { ".".to_owned() } else { slug }
}
}
}
impl Resource for ArticleRedirectResource {
fn allow(&self) -> Vec<hyper::Method> {
use hyper::Method::*;
vec![Options, Head, Get, Put]
}
fn head(&self) -> ResponseFuture {
Box::new(futures::finished(Response::new()
.with_status(hyper::StatusCode::TemporaryRedirect)
.with_header(Location::new(self.slug.clone()))
))
}
fn get(self: Box<Self>) -> ResponseFuture {
Box::new(self.head()
.and_then(move |head| {
Ok(head
.with_body(format!("Moved to {}", self.slug)))
}))
}
fn put(self: Box<Self>, _body: hyper::Body, _identity: Option<String>) -> ResponseFuture {
Box::new(self.head()
.and_then(move |head| {
Ok(head
.with_body(format!("Moved to {}", self.slug)))
}))
}
}

View file

@ -1,6 +1,5 @@
pub mod pagination; pub mod pagination;
mod article_redirect_resource;
mod article_revision_resource; mod article_revision_resource;
mod article_resource; mod article_resource;
mod changes_resource; mod changes_resource;
@ -9,7 +8,6 @@ mod search_resource;
mod sitemap_resource; mod sitemap_resource;
mod temporary_redirect_resource; mod temporary_redirect_resource;
pub use self::article_redirect_resource::ArticleRedirectResource;
pub use self::article_revision_resource::ArticleRevisionResource; pub use self::article_revision_resource::ArticleRevisionResource;
pub use self::article_resource::ArticleResource; pub use self::article_resource::ArticleResource;
pub use self::changes_resource::{ChangesLookup, ChangesResource}; pub use self::changes_resource::{ChangesLookup, ChangesResource};

View file

@ -13,6 +13,17 @@ impl TemporaryRedirectResource {
pub fn new(location: String) -> Self { pub fn new(location: String) -> Self {
Self { location } Self { location }
} }
pub fn from_slug<S: AsRef<str>>(slug: S) -> Self {
Self {
location:
if slug.as_ref().is_empty() {
"."
} else {
slug.as_ref()
}.to_owned()
}
}
} }
impl Resource for TemporaryRedirectResource { impl Resource for TemporaryRedirectResource {

View file

@ -170,7 +170,7 @@ impl WikiLookup {
let slugified_slug = slugify(&slug); let slugified_slug = slugify(&slug);
if slugified_slug != slug { if slugified_slug != slug {
return Box::new(finished(Some( return Box::new(finished(Some(
Box::new(ArticleRedirectResource::new(slugified_slug)) as BoxResource Box::new(TemporaryRedirectResource::from_slug(slugified_slug)) as BoxResource
))); )));
} }
@ -186,7 +186,7 @@ impl WikiLookup {
SlugLookup::Hit { article_id, revision } => SlugLookup::Hit { article_id, revision } =>
Box::new(ArticleResource::new(state, article_id, revision, edit)) as BoxResource, Box::new(ArticleResource::new(state, article_id, revision, edit)) as BoxResource,
SlugLookup::Redirect(slug) => SlugLookup::Redirect(slug) =>
Box::new(ArticleRedirectResource::new(slug)) as BoxResource, Box::new(TemporaryRedirectResource::from_slug(slug)) as BoxResource,
}))) })))
) )
} }