2017-09-15 18:28:23 +03:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2017-09-17 16:43:31 +03:00
|
|
|
use futures::{Future, finished};
|
2017-09-15 18:28:23 +03:00
|
|
|
|
|
|
|
use assets::*;
|
|
|
|
use article_resource::ArticleResource;
|
2017-09-20 12:54:26 +03:00
|
|
|
use article_redirect_resource::ArticleRedirectResource;
|
2017-09-15 18:28:23 +03:00
|
|
|
use state::State;
|
|
|
|
use web::{Lookup, Resource};
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref LOOKUP_MAP: HashMap<String, Box<Fn() -> Box<Resource + Sync + Send> + Sync + Send>> = {
|
|
|
|
let mut lookup_map = HashMap::new();
|
|
|
|
|
|
|
|
lookup_map.insert(
|
|
|
|
format!("/_assets/style-{}.css", StyleCss::checksum()),
|
|
|
|
Box::new(|| Box::new(StyleCss) as Box<Resource + Sync + Send>)
|
|
|
|
as Box<Fn() -> Box<Resource + Sync + Send> + Sync + Send>
|
|
|
|
);
|
|
|
|
|
|
|
|
lookup_map.insert(
|
|
|
|
format!("/_assets/script-{}.js", ScriptJs::checksum()),
|
|
|
|
Box::new(|| Box::new(ScriptJs) as Box<Resource + Sync + Send>)
|
|
|
|
as Box<Fn() -> Box<Resource + Sync + Send> + Sync + Send>
|
|
|
|
);
|
|
|
|
|
|
|
|
lookup_map.insert(
|
|
|
|
format!("/_assets/amatic-sc-v9-latin-regular.woff"),
|
|
|
|
Box::new(|| Box::new(AmaticFont) as Box<Resource + Sync + Send>)
|
|
|
|
as Box<Fn() -> Box<Resource + Sync + Send> + Sync + Send>
|
|
|
|
);
|
|
|
|
|
|
|
|
lookup_map
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct WikiLookup {
|
|
|
|
state: State
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WikiLookup {
|
|
|
|
pub fn new(state: State) -> WikiLookup {
|
|
|
|
WikiLookup { state }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Lookup for WikiLookup {
|
|
|
|
type Resource = Box<Resource + Send + Sync>;
|
|
|
|
type Error = Box<::std::error::Error + Send + Sync>;
|
2017-09-17 13:08:42 +03:00
|
|
|
type Future = Box<Future<Item = Option<Self::Resource>, Error = Self::Error>>;
|
2017-09-15 18:28:23 +03:00
|
|
|
|
|
|
|
fn lookup(&self, path: &str, _query: Option<&str>, _fragment: Option<&str>) -> Self::Future {
|
|
|
|
assert!(path.starts_with("/"));
|
|
|
|
|
|
|
|
if path.starts_with("/_") {
|
|
|
|
// Reserved namespace
|
|
|
|
|
2017-09-17 16:43:31 +03:00
|
|
|
return Box::new(finished(
|
2017-09-15 18:28:23 +03:00
|
|
|
LOOKUP_MAP.get(path).map(|x| x())
|
2017-09-17 12:45:35 +03:00
|
|
|
));
|
2017-09-15 18:28:23 +03:00
|
|
|
}
|
|
|
|
|
2017-09-17 12:27:50 +03:00
|
|
|
let mut split = path[1..].split('/');
|
|
|
|
|
|
|
|
let slug = split.next().expect("Always at least one element");
|
|
|
|
|
|
|
|
if split.next() != None {
|
|
|
|
// Currently disallow any URLs of the form /slug/...
|
2017-09-17 16:43:31 +03:00
|
|
|
return Box::new(finished(None));
|
2017-09-17 12:27:50 +03:00
|
|
|
}
|
|
|
|
|
2017-09-20 12:54:26 +03:00
|
|
|
let state = self.state.clone();
|
|
|
|
|
|
|
|
use state::SlugLookup;
|
|
|
|
Box::new(self.state.lookup_slug(slug.to_owned())
|
|
|
|
.and_then(|x| Ok(match x {
|
|
|
|
SlugLookup::Miss => None,
|
|
|
|
SlugLookup::Hit { article_id, revision } =>
|
|
|
|
Some(Box::new(ArticleResource::new(state, article_id, revision))
|
|
|
|
as Box<Resource + Sync + Send>),
|
|
|
|
SlugLookup::Redirect(slug) =>
|
|
|
|
Some(Box::new(ArticleRedirectResource::new(slug))
|
|
|
|
as Box<Resource + Sync + Send>)
|
|
|
|
})))
|
2017-09-15 18:28:23 +03:00
|
|
|
}
|
|
|
|
}
|