surwiki/src/site.rs

114 lines
3.4 KiB
Rust
Raw Permalink Normal View History

2017-09-08 15:43:47 +03:00
// #[derive(BartDisplay)] can cause unused extern crates warning:
#![allow(unused_extern_crates)]
use std::fmt;
2017-08-20 21:46:08 +03:00
use futures::{self, Future};
2017-09-15 16:09:51 +03:00
use hyper::header::ContentType;
2017-08-20 21:46:08 +03:00
use hyper::mime;
2017-08-20 21:24:10 +03:00
use hyper::server::*;
use hyper;
2017-09-15 18:28:23 +03:00
use assets::StyleCss;
use web::Lookup;
use wiki_lookup::WikiLookup;
2017-09-01 17:34:24 +03:00
2017-08-20 21:46:08 +03:00
lazy_static! {
static ref TEXT_HTML: mime::Mime = "text/html;charset=utf-8".parse().unwrap();
}
#[derive(BartDisplay)]
#[template = "templates/layout.html"]
2017-09-15 18:28:23 +03:00
pub struct Layout<'a, T: 'a + fmt::Display> {
2017-10-13 17:05:22 +03:00
pub base: Option<&'a str>,
pub title: &'a str,
pub body: &'a T,
pub style_css_checksum: &'a str,
}
2017-08-20 23:39:52 +03:00
#[derive(BartDisplay)]
#[template = "templates/error/404.html"]
2017-08-20 23:39:52 +03:00
struct NotFound;
#[derive(BartDisplay)]
#[template = "templates/error/500.html"]
2017-08-20 23:39:52 +03:00
struct InternalServerError;
2017-08-20 21:24:10 +03:00
pub struct Site {
root: WikiLookup,
}
impl Site {
2017-09-15 18:28:23 +03:00
pub fn new(root: WikiLookup) -> Site {
Site { root }
}
2017-10-13 17:05:22 +03:00
fn not_found(base: Option<&str>) -> Response {
Response::new()
.with_header(ContentType(TEXT_HTML.clone()))
.with_body(Layout {
2017-10-13 17:05:22 +03:00
base: base,
title: "Not found",
body: &NotFound,
style_css_checksum: StyleCss::checksum(),
}.to_string())
.with_status(hyper::StatusCode::NotFound)
}
2017-10-13 17:05:22 +03:00
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 {
2017-10-13 17:05:22 +03:00
base,
title: "Internal server error",
body: &InternalServerError,
style_css_checksum: StyleCss::checksum(),
}.to_string())
.with_status(hyper::StatusCode::InternalServerError)
}
2017-08-20 21:24:10 +03:00
}
2017-10-13 17:05:22 +03:00
fn root_base_from_request_uri(path: &str) -> Option<String> {
assert!(path.starts_with("/"));
let slashes = path[1..].matches('/').count();
match slashes {
0 => None,
n => Some(::std::iter::repeat("../").take(n).collect())
}
}
2017-08-20 21:24:10 +03:00
impl Service for Site {
type Request = Request;
type Response = Response;
type Error = hyper::Error;
type Future = Box<futures::Future<Item = Response, Error = Self::Error>>;
2017-08-20 21:24:10 +03:00
fn call(&self, req: Request) -> Self::Future {
2017-09-05 13:05:56 +03:00
let (method, uri, _http_version, _headers, body) = req.deconstruct();
println!("{} {}", method, uri);
2017-10-13 17:05:22 +03:00
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) => {
use hyper::Method::*;
match method {
Options => Box::new(futures::finished(resource.options())),
Head => resource.head(),
Get => resource.get(),
2017-09-05 18:07:57 +03:00
Put => resource.put(body),
_ => Box::new(futures::finished(resource.method_not_allowed()))
2017-08-21 00:44:52 +03:00
}
},
2017-10-13 17:05:22 +03:00
None => Box::new(futures::finished(Self::not_found(base.as_ref().map(|x| &**x))))
})
2017-10-13 17:05:22 +03:00
.or_else(move |err| Ok(Self::internal_server_error(base2.as_ref().map(|x| &**x), err)))
)
2017-08-20 21:24:10 +03:00
}
}