Allow static files to be served using mdbook serve

This commit is contained in:
Richard Dodd 2017-04-30 14:39:29 +01:00
parent 435682e95c
commit fccb04c862
2 changed files with 22 additions and 4 deletions

View File

@ -33,6 +33,7 @@ crossbeam = { version = "0.2.8", optional = true }
# Serve feature # Serve feature
iron = { version = "0.5", optional = true } iron = { version = "0.5", optional = true }
mount = { version = "0.3", optional = true }
staticfile = { version = "0.4", optional = true } staticfile = { version = "0.4", optional = true }
ws = { version = "0.6", optional = true} ws = { version = "0.6", optional = true}
@ -46,7 +47,7 @@ debug = []
output = [] output = []
regenerate-css = [] regenerate-css = []
watch = ["notify", "time", "crossbeam"] watch = ["notify", "time", "crossbeam"]
serve = ["iron", "staticfile", "ws"] serve = ["iron", "mount", "staticfile", "ws"]
[[bin]] [[bin]]
doc = false doc = false

View File

@ -17,6 +17,8 @@ extern crate crossbeam;
#[cfg(feature = "serve")] #[cfg(feature = "serve")]
extern crate iron; extern crate iron;
#[cfg(feature = "serve")] #[cfg(feature = "serve")]
extern crate mount;
#[cfg(feature = "serve")]
extern crate staticfile; extern crate staticfile;
#[cfg(feature = "serve")] #[cfg(feature = "serve")]
extern crate ws; 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("-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("-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("-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") .subcommand(SubCommand::with_name("test")
.about("Test that code samples compile")) .about("Test that code samples compile"))
.get_matches(); .get_matches();
@ -235,6 +238,7 @@ fn serve(args: &ArgMatches) -> Result<(), Box<Error>> {
let interface = args.value_of("interface").unwrap_or("localhost"); let interface = args.value_of("interface").unwrap_or("localhost");
let public_address = args.value_of("address").unwrap_or(interface); let public_address = args.value_of("address").unwrap_or(interface);
let open_browser = args.is_present("open"); let open_browser = args.is_present("open");
let static_objs = args.values_of("static");
let address = format!("{}:{}", interface, port); let address = format!("{}:{}", interface, port);
let ws_address = format!("{}:{}", interface, ws_port); let ws_address = format!("{}:{}", interface, ws_port);
@ -257,8 +261,21 @@ fn serve(args: &ArgMatches) -> Result<(), Box<Error>> {
try!(book.build()); try!(book.build());
let staticfile = staticfile::Static::new(book.get_dest()); let mut mount = mount::Mount::new();
let iron = iron::Iron::new(staticfile); 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 _iron = iron.http(&*address).unwrap();
let ws_server = ws::WebSocket::new(|_| { let ws_server = ws::WebSocket::new(|_| {