Merge pull request #332 from budziq/silence_404s
error spewing on iron 404 errors
This commit is contained in:
commit
69b3e2b5cb
|
@ -13,14 +13,6 @@ extern crate time;
|
||||||
#[cfg(feature = "watch")]
|
#[cfg(feature = "watch")]
|
||||||
extern crate crossbeam;
|
extern crate crossbeam;
|
||||||
|
|
||||||
// Dependencies for the Serve feature
|
|
||||||
#[cfg(feature = "serve")]
|
|
||||||
extern crate iron;
|
|
||||||
#[cfg(feature = "serve")]
|
|
||||||
extern crate staticfile;
|
|
||||||
#[cfg(feature = "serve")]
|
|
||||||
extern crate ws;
|
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
|
@ -93,7 +85,7 @@ fn main() {
|
||||||
#[cfg(feature = "watch")]
|
#[cfg(feature = "watch")]
|
||||||
("watch", Some(sub_matches)) => watch(sub_matches),
|
("watch", Some(sub_matches)) => watch(sub_matches),
|
||||||
#[cfg(feature = "serve")]
|
#[cfg(feature = "serve")]
|
||||||
("serve", Some(sub_matches)) => serve(sub_matches),
|
("serve", Some(sub_matches)) => serve::serve(sub_matches),
|
||||||
("test", Some(sub_matches)) => test(sub_matches),
|
("test", Some(sub_matches)) => test(sub_matches),
|
||||||
(_, _) => unreachable!(),
|
(_, _) => unreachable!(),
|
||||||
};
|
};
|
||||||
|
@ -233,17 +225,42 @@ fn watch(args: &ArgMatches) -> Result<(), Box<Error>> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "serve")]
|
||||||
|
mod serve {
|
||||||
|
extern crate iron;
|
||||||
|
extern crate staticfile;
|
||||||
|
extern crate ws;
|
||||||
|
|
||||||
|
use std;
|
||||||
|
use std::path::Path;
|
||||||
|
use std::error::Error;
|
||||||
|
use self::iron::{Iron, AfterMiddleware, IronResult, IronError, Request, Response, status, Set, Chain};
|
||||||
|
use clap::ArgMatches;
|
||||||
|
use mdbook::MDBook;
|
||||||
|
|
||||||
|
use {get_book_dir, open, trigger_on_change};
|
||||||
|
|
||||||
|
struct ErrorRecover;
|
||||||
|
|
||||||
|
impl AfterMiddleware for ErrorRecover {
|
||||||
|
fn catch(&self, _: &mut Request, err: IronError) -> IronResult<Response> {
|
||||||
|
match err.response.status {
|
||||||
|
// each error will result in 404 response
|
||||||
|
Some(_) => Ok(err.response.set(status::NotFound)),
|
||||||
|
_ => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Watch command implementation
|
// Watch command implementation
|
||||||
#[cfg(feature = "serve")]
|
pub fn serve(args: &ArgMatches) -> Result<(), Box<Error>> {
|
||||||
fn serve(args: &ArgMatches) -> Result<(), Box<Error>> {
|
|
||||||
const RELOAD_COMMAND: &'static str = "reload";
|
const RELOAD_COMMAND: &'static str = "reload";
|
||||||
|
|
||||||
let book_dir = get_book_dir(args);
|
let book_dir = get_book_dir(args);
|
||||||
let book = MDBook::new(&book_dir).read_config()?;
|
let book = MDBook::new(&book_dir).read_config()?;
|
||||||
|
|
||||||
let mut book = match args.value_of("dest-dir") {
|
let mut book = match args.value_of("dest-dir") {
|
||||||
Some(dest_dir) => book.with_destination(dest_dir),
|
Some(dest_dir) => book.with_destination(Path::new(dest_dir)),
|
||||||
None => book,
|
None => book,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -286,9 +303,10 @@ fn serve(args: &ArgMatches) -> Result<(), Box<Error>> {
|
||||||
|
|
||||||
book.build()?;
|
book.build()?;
|
||||||
|
|
||||||
let staticfile = staticfile::Static::new(book.get_destination().expect("destination is present, checked before"));
|
let mut chain = Chain::new(staticfile::Static::new(book.get_destination()
|
||||||
let iron = iron::Iron::new(staticfile);
|
.expect("destination is present, checked before")));
|
||||||
let _iron = iron.http(&*address).unwrap();
|
chain.link_after(ErrorRecover);
|
||||||
|
let _iron = Iron::new(chain).http(&*address).unwrap();
|
||||||
|
|
||||||
let ws_server = ws::WebSocket::new(|_| |_| Ok(())).unwrap();
|
let ws_server = ws::WebSocket::new(|_| |_| Ok(())).unwrap();
|
||||||
|
|
||||||
|
@ -314,7 +332,7 @@ fn serve(args: &ArgMatches) -> Result<(), Box<Error>> {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn test(args: &ArgMatches) -> Result<(), Box<Error>> {
|
fn test(args: &ArgMatches) -> Result<(), Box<Error>> {
|
||||||
let book_dir = get_book_dir(args);
|
let book_dir = get_book_dir(args);
|
||||||
|
|
Loading…
Reference in New Issue