2017-06-26 03:41:23 +08:00
|
|
|
extern crate iron;
|
|
|
|
extern crate staticfile;
|
|
|
|
extern crate ws;
|
|
|
|
|
2018-07-24 01:45:01 +08:00
|
|
|
use self::iron::{
|
|
|
|
status, AfterMiddleware, Chain, Iron, IronError, IronResult, Request, Response, Set,
|
|
|
|
};
|
2017-10-03 19:40:23 +08:00
|
|
|
use clap::{App, ArgMatches, SubCommand};
|
2017-12-13 09:04:24 +08:00
|
|
|
use mdbook::errors::*;
|
2018-07-24 01:45:01 +08:00
|
|
|
use mdbook::utils;
|
|
|
|
use mdbook::MDBook;
|
|
|
|
use std;
|
2017-06-26 05:44:28 +08:00
|
|
|
#[cfg(feature = "watch")]
|
|
|
|
use watch;
|
2018-07-24 01:45:01 +08:00
|
|
|
use {get_book_dir, open};
|
2017-06-26 03:41:23 +08:00
|
|
|
|
|
|
|
struct ErrorRecover;
|
|
|
|
|
2017-06-27 13:59:50 +08:00
|
|
|
// Create clap subcommand arguments
|
|
|
|
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
|
|
|
|
SubCommand::with_name("serve")
|
2017-12-13 09:04:24 +08:00
|
|
|
.about("Serve the book at http://localhost:3000. Rebuild and reload on change.")
|
2017-10-03 19:40:23 +08:00
|
|
|
.arg_from_usage(
|
2017-12-13 09:04:24 +08:00
|
|
|
"[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'",
|
2017-10-03 19:40:23 +08:00
|
|
|
)
|
2017-06-27 13:59:50 +08:00
|
|
|
.arg_from_usage("-p, --port=[port] 'Use another port{n}(Defaults to 3000)'")
|
2017-10-03 19:40:23 +08:00
|
|
|
.arg_from_usage(
|
2017-12-13 09:04:24 +08:00
|
|
|
"-w, --websocket-port=[ws-port] 'Use another port for the websocket connection \
|
|
|
|
(livereload){n}(Defaults to 3001)'",
|
2017-10-03 19:40:23 +08:00
|
|
|
)
|
|
|
|
.arg_from_usage(
|
|
|
|
"-i, --interface=[interface] 'Interface to listen on{n}(Defaults to localhost)'",
|
|
|
|
)
|
|
|
|
.arg_from_usage(
|
2017-12-13 09:04:24 +08:00
|
|
|
"-a, --address=[address] 'Address that the browser can reach the websocket server \
|
|
|
|
from{n}(Defaults to the interface address)'",
|
2017-10-03 19:40:23 +08:00
|
|
|
)
|
2017-06-27 13:59:50 +08:00
|
|
|
.arg_from_usage("-o, --open 'Open the book server in a web browser'")
|
2017-06-26 03:41:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Watch command implementation
|
2017-06-27 13:59:50 +08:00
|
|
|
pub fn execute(args: &ArgMatches) -> Result<()> {
|
2017-06-26 03:41:23 +08:00
|
|
|
let book_dir = get_book_dir(args);
|
2017-11-18 20:41:04 +08:00
|
|
|
let mut book = MDBook::load(&book_dir)?;
|
2017-06-26 03:41:23 +08:00
|
|
|
|
|
|
|
let port = args.value_of("port").unwrap_or("3000");
|
|
|
|
let ws_port = args.value_of("websocket-port").unwrap_or("3001");
|
|
|
|
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 address = format!("{}:{}", interface, port);
|
|
|
|
let ws_address = format!("{}:{}", interface, ws_port);
|
|
|
|
|
2018-01-07 22:10:48 +08:00
|
|
|
let livereload_url = format!("ws://{}:{}", public_address, ws_port);
|
|
|
|
book.config
|
|
|
|
.set("output.html.livereload-url", &livereload_url)?;
|
2017-06-26 03:41:23 +08:00
|
|
|
|
|
|
|
book.build()?;
|
|
|
|
|
2018-01-07 22:10:48 +08:00
|
|
|
let mut chain = Chain::new(staticfile::Static::new(book.build_dir_for("html")));
|
2017-06-26 03:41:23 +08:00
|
|
|
chain.link_after(ErrorRecover);
|
2017-12-13 09:04:24 +08:00
|
|
|
let _iron = Iron::new(chain)
|
|
|
|
.http(&*address)
|
|
|
|
.chain_err(|| "Unable to launch the server")?;
|
2017-06-26 03:41:23 +08:00
|
|
|
|
2017-12-13 09:04:24 +08:00
|
|
|
let ws_server =
|
|
|
|
ws::WebSocket::new(|_| |_| Ok(())).chain_err(|| "Unable to start the websocket")?;
|
2017-06-26 03:41:23 +08:00
|
|
|
|
|
|
|
let broadcaster = ws_server.broadcaster();
|
|
|
|
|
2017-11-18 20:41:04 +08:00
|
|
|
std::thread::spawn(move || {
|
|
|
|
ws_server.listen(&*ws_address).unwrap();
|
|
|
|
});
|
2017-06-26 03:41:23 +08:00
|
|
|
|
|
|
|
let serving_url = format!("http://{}", address);
|
2018-01-07 22:10:48 +08:00
|
|
|
info!("Serving on: {}", serving_url);
|
2017-06-26 03:41:23 +08:00
|
|
|
|
|
|
|
if open_browser {
|
|
|
|
open(serving_url);
|
|
|
|
}
|
|
|
|
|
2017-06-26 05:44:28 +08:00
|
|
|
#[cfg(feature = "watch")]
|
2017-12-13 09:04:24 +08:00
|
|
|
watch::trigger_on_change(&mut book, move |path, book_dir| {
|
2018-01-07 22:10:48 +08:00
|
|
|
info!("File changed: {:?}", path);
|
|
|
|
info!("Building book...");
|
|
|
|
|
2018-01-03 19:32:49 +08:00
|
|
|
// FIXME: This area is really ugly because we need to re-set livereload :(
|
2018-01-07 22:10:48 +08:00
|
|
|
|
|
|
|
let livereload_url = livereload_url.clone();
|
2018-01-03 19:32:49 +08:00
|
|
|
|
|
|
|
let result = MDBook::load(&book_dir)
|
2018-01-07 22:10:48 +08:00
|
|
|
.and_then(move |mut b| {
|
|
|
|
b.config.set("output.html.livereload-url", &livereload_url)?;
|
|
|
|
Ok(b)
|
2018-01-03 19:32:49 +08:00
|
|
|
})
|
2018-01-07 22:10:48 +08:00
|
|
|
.and_then(|b| b.build());
|
2018-01-03 19:32:49 +08:00
|
|
|
|
|
|
|
if let Err(e) = result {
|
|
|
|
error!("Unable to load the book");
|
2018-01-07 22:10:48 +08:00
|
|
|
utils::log_backtrace(&e);
|
2018-01-03 19:32:49 +08:00
|
|
|
} else {
|
2018-01-07 22:10:48 +08:00
|
|
|
let _ = broadcaster.send("reload");
|
2017-06-26 03:41:23 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-06-26 06:31:42 +08:00
|
|
|
|
2017-06-27 13:59:50 +08:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
2017-06-26 06:31:42 +08:00
|
|
|
}
|