2018-07-26 01:20:48 +08:00
|
|
|
#[cfg(feature = "watch")]
|
|
|
|
use super::watch;
|
2019-05-26 02:50:41 +08:00
|
|
|
use crate::{get_book_dir, open};
|
2018-08-03 04:48:22 +08:00
|
|
|
use clap::{App, Arg, ArgMatches, SubCommand};
|
2019-11-13 10:06:11 +08:00
|
|
|
use iron::headers;
|
2019-05-26 02:50:41 +08:00
|
|
|
use iron::{status, AfterMiddleware, Chain, Iron, IronError, IronResult, Request, Response, Set};
|
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;
|
2017-06-26 03:41:23 +08:00
|
|
|
|
|
|
|
struct ErrorRecover;
|
|
|
|
|
2019-11-13 10:06:11 +08:00
|
|
|
struct NoCache;
|
|
|
|
|
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")
|
2018-08-03 04:48:22 +08:00
|
|
|
.about("Serves a book at http://localhost:3000, and rebuilds it on changes")
|
2017-10-03 19:40:23 +08:00
|
|
|
.arg_from_usage(
|
2018-08-03 04:48:22 +08:00
|
|
|
"-d, --dest-dir=[dest-dir] 'Output directory for the book{n}\
|
2018-09-06 23:24:42 +08:00
|
|
|
Relative paths are interpreted relative to the book's root directory.{n}\
|
|
|
|
If omitted, mdBook uses build.build-dir from book.toml or defaults to `./book`.'",
|
2017-10-03 19:40:23 +08:00
|
|
|
)
|
|
|
|
.arg_from_usage(
|
2018-08-03 04:48:22 +08:00
|
|
|
"[dir] 'Root directory for the book{n}\
|
|
|
|
(Defaults to the Current Directory when omitted)'",
|
2017-10-03 19:40:23 +08:00
|
|
|
)
|
2018-08-03 04:48:22 +08:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("hostname")
|
|
|
|
.short("n")
|
|
|
|
.long("hostname")
|
|
|
|
.takes_value(true)
|
|
|
|
.default_value("localhost")
|
|
|
|
.empty_values(false)
|
|
|
|
.help("Hostname to listen on for HTTP connections"),
|
2017-10-03 19:40:23 +08:00
|
|
|
)
|
2018-08-03 04:48:22 +08:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("port")
|
|
|
|
.short("p")
|
|
|
|
.long("port")
|
|
|
|
.takes_value(true)
|
|
|
|
.default_value("3000")
|
|
|
|
.empty_values(false)
|
|
|
|
.help("Port to use for HTTP connections"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("websocket-hostname")
|
|
|
|
.long("websocket-hostname")
|
|
|
|
.takes_value(true)
|
|
|
|
.empty_values(false)
|
|
|
|
.help(
|
|
|
|
"Hostname to connect to for WebSockets connections (Defaults to the HTTP hostname)",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("websocket-port")
|
|
|
|
.short("w")
|
|
|
|
.long("websocket-port")
|
|
|
|
.takes_value(true)
|
|
|
|
.default_value("3001")
|
|
|
|
.empty_values(false)
|
|
|
|
.help("Port to use for WebSockets livereload connections"),
|
2017-10-03 19:40:23 +08:00
|
|
|
)
|
2018-08-03 04:48:22 +08:00
|
|
|
.arg_from_usage("-o, --open 'Opens 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
|
|
|
|
2018-08-03 04:48:22 +08:00
|
|
|
let port = args.value_of("port").unwrap();
|
|
|
|
let ws_port = args.value_of("websocket-port").unwrap();
|
|
|
|
let hostname = args.value_of("hostname").unwrap();
|
2019-01-10 00:59:45 +08:00
|
|
|
let public_address = args.value_of("websocket-hostname").unwrap_or(hostname);
|
2017-06-26 03:41:23 +08:00
|
|
|
let open_browser = args.is_present("open");
|
|
|
|
|
2018-08-03 04:48:22 +08:00
|
|
|
let address = format!("{}:{}", hostname, port);
|
|
|
|
let ws_address = format!("{}:{}", hostname, ws_port);
|
2017-06-26 03:41:23 +08:00
|
|
|
|
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
|
|
|
|
2018-08-03 04:48:22 +08:00
|
|
|
if let Some(dest_dir) = args.value_of("dest-dir") {
|
|
|
|
book.config.build.build_dir = dest_dir.into();
|
|
|
|
}
|
|
|
|
|
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")));
|
2019-11-13 10:06:11 +08:00
|
|
|
chain.link_after(NoCache);
|
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")]
|
2019-02-01 02:31:02 +08:00
|
|
|
watch::trigger_on_change(&book, move |paths, book_dir| {
|
|
|
|
info!("Files changed: {:?}", paths);
|
2018-01-07 22:10:48 +08:00
|
|
|
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
|
|
|
|
2018-01-03 19:32:49 +08:00
|
|
|
let result = MDBook::load(&book_dir)
|
2018-08-03 04:48:22 +08:00
|
|
|
.and_then(|mut b| {
|
2018-08-21 23:58:44 +08:00
|
|
|
b.config
|
|
|
|
.set("output.html.livereload-url", &livereload_url)?;
|
2018-01-07 22:10:48 +08:00
|
|
|
Ok(b)
|
2019-05-05 22:57:43 +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
|
|
|
|
2019-11-13 10:06:11 +08:00
|
|
|
impl AfterMiddleware for NoCache {
|
|
|
|
fn after(&self, _: &mut Request, mut res: Response) -> IronResult<Response> {
|
|
|
|
res.headers.set(headers::CacheControl(vec![
|
|
|
|
headers::CacheDirective::NoStore,
|
|
|
|
headers::CacheDirective::MaxAge(0u32),
|
|
|
|
]));
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|