Merge pull request #332 from budziq/silence_404s

error spewing on iron 404 errors
This commit is contained in:
Mathieu David 2017-06-23 11:27:31 +02:00 committed by GitHub
commit 69b3e2b5cb
1 changed files with 76 additions and 58 deletions

View File

@ -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(())
} }
// Watch command implementation
#[cfg(feature = "serve")] #[cfg(feature = "serve")]
fn serve(args: &ArgMatches) -> Result<(), Box<Error>> { 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
pub 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();
@ -313,9 +331,9 @@ 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);
let mut book = MDBook::new(&book_dir).read_config()?; let mut book = MDBook::new(&book_dir).read_config()?;