From fccb04c8622a35aeb8ff3d65dfb2199d7c143143 Mon Sep 17 00:00:00 2001 From: Richard Dodd Date: Sun, 30 Apr 2017 14:39:29 +0100 Subject: [PATCH] Allow static files to be served using mdbook serve --- Cargo.toml | 3 ++- src/bin/mdbook.rs | 23 ++++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7f320169..12896a15 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,7 @@ crossbeam = { version = "0.2.8", optional = true } # Serve feature iron = { version = "0.5", optional = true } +mount = { version = "0.3", optional = true } staticfile = { version = "0.4", optional = true } ws = { version = "0.6", optional = true} @@ -46,7 +47,7 @@ debug = [] output = [] regenerate-css = [] watch = ["notify", "time", "crossbeam"] -serve = ["iron", "staticfile", "ws"] +serve = ["iron", "mount", "staticfile", "ws"] [[bin]] doc = false diff --git a/src/bin/mdbook.rs b/src/bin/mdbook.rs index 3af3ada4..25de32bb 100644 --- a/src/bin/mdbook.rs +++ b/src/bin/mdbook.rs @@ -17,6 +17,8 @@ extern crate crossbeam; #[cfg(feature = "serve")] extern crate iron; #[cfg(feature = "serve")] +extern crate mount; +#[cfg(feature = "serve")] extern crate staticfile; #[cfg(feature = "serve")] extern crate ws; @@ -78,7 +80,8 @@ fn main() { .arg_from_usage("-w, --websocket-port=[ws-port] 'Use another port for the websocket connection (livereload){n}(Defaults to 3001)'") .arg_from_usage("-i, --interface=[interface] 'Interface to listen on{n}(Defaults to localhost)'") .arg_from_usage("-a, --address=[address] 'Address that the browser can reach the websocket server from{n}(Defaults to the interface address)'") - .arg_from_usage("-o, --open 'Open the book server in a web browser'")) + .arg_from_usage("-o, --open 'Open the book server in a web browser'") + .arg_from_usage("-s, --static=[static]... 'Include a file or folder as static content on the webserver'")) .subcommand(SubCommand::with_name("test") .about("Test that code samples compile")) .get_matches(); @@ -235,6 +238,7 @@ fn serve(args: &ArgMatches) -> Result<(), Box> { let interface = args.value_of("interface").unwrap_or("localhost"); let public_address = args.value_of("address").unwrap_or(interface); let open_browser = args.is_present("open"); + let static_objs = args.values_of("static"); let address = format!("{}:{}", interface, port); let ws_address = format!("{}:{}", interface, ws_port); @@ -257,8 +261,21 @@ fn serve(args: &ArgMatches) -> Result<(), Box> { try!(book.build()); - let staticfile = staticfile::Static::new(book.get_dest()); - let iron = iron::Iron::new(staticfile); + let mut mount = mount::Mount::new(); + mount.mount("/", staticfile::Static::new(book.get_dest())); + if let Some(values) = static_objs { + for value in values { + let path = Path::new(value); + if path.exists() { + if let Some(filename) = path.file_name() { + mount.mount(&filename.to_string_lossy(), staticfile::Static::new(path)); + } + } else { + println!("Static path {} not found on file system, skipping...", path.display()); + } + } + } + let iron = iron::Iron::new(mount); let _iron = iron.http(&*address).unwrap(); let ws_server = ws::WebSocket::new(|_| {