diff --git a/docs/content/documentation/getting-started/cli-usage.md b/docs/content/documentation/getting-started/cli-usage.md index 44b0c410..c0c1e939 100644 --- a/docs/content/documentation/getting-started/cli-usage.md +++ b/docs/content/documentation/getting-started/cli-usage.md @@ -46,11 +46,14 @@ $ gutenberg build --output-dir $DOCUMENT_ROOT This will build and serve the site using a local server. You can also specify the interface/port combination to use if you want something different than the default (`127.0.0.1:1111`). +You can also specify different addresses for the interface and base_url using `-u`/`--base-url`. + ```bash $ gutenberg serve $ gutenberg serve --port 2000 $ gutenberg serve --interface 0.0.0.0 $ gutenberg serve --interface 0.0.0.0 --port 2000 +$ gutenberg serve --interface 0.0.0.0 --base-url 127.0.0.1 $ gutenberg serve --interface 0.0.0.0 --port 2000 --output-dir www/public ``` diff --git a/src/cli.rs b/src/cli.rs index b433f8e8..70fcfd6e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -54,6 +54,12 @@ pub fn build_cli() -> App<'static, 'static> { .default_value("public") .takes_value(true) .help("Outputs the generated site in the given path"), + Arg::with_name("base_url") + .short("u") + .long("base-url") + .default_value("127.0.0.1") + .takes_value(true) + .help("Changes the base_url"), ]), ]) } diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index 22ff97f3..dac394a7 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -80,16 +80,18 @@ fn rebuild_done_handling(broadcaster: &Sender, res: Result<()>, reload_path: &st } } -fn create_new_site(interface: &str, port: &str, output_dir: &str, config_file: &str) -> Result<(Site, String)> { +fn create_new_site(interface: &str, port: &str, output_dir: &str, base_url: &str, config_file: &str) -> Result<(Site, String)> { let mut site = Site::new(env::current_dir().unwrap(), config_file)?; + let base_address = format!("{}:{}", base_url, port); let address = format!("{}:{}", interface, port); - // Override the base url so links work in localhost + site.config.base_url = if site.config.base_url.ends_with('/') { - format!("http://{}/", address) + format!("http://{}/", base_address) } else { - format!("http://{}", address) + format!("http://{}", base_address) }; + site.set_output_path(output_dir); site.load()?; site.enable_live_reload(); @@ -99,9 +101,9 @@ fn create_new_site(interface: &str, port: &str, output_dir: &str, config_file: & Ok((site, address)) } -pub fn serve(interface: &str, port: &str, output_dir: &str, config_file: &str) -> Result<()> { +pub fn serve(interface: &str, port: &str, output_dir: &str, base_url: &str, config_file: &str) -> Result<()> { let start = Instant::now(); - let (mut site, address) = create_new_site(interface, port, output_dir, config_file)?; + let (mut site, address) = create_new_site(interface, port, output_dir, base_url, config_file)?; console::report_elapsed_time(start); // Setup watchers @@ -214,7 +216,7 @@ pub fn serve(interface: &str, port: &str, output_dir: &str, config_file: &str) - }, (ChangeKind::Config, _) => { console::info(&format!("-> Config changed. The whole site will be reloaded. The browser needs to be refreshed to make the changes visible.")); - site = create_new_site(interface, port, output_dir, config_file).unwrap().0; + site = create_new_site(interface, port, output_dir, base_url, config_file).unwrap().0; } }; console::report_elapsed_time(start); diff --git a/src/main.rs b/src/main.rs index e7c98446..5d0fa359 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,8 +57,9 @@ fn main() { let interface = matches.value_of("interface").unwrap_or("127.0.0.1"); let port = matches.value_of("port").unwrap_or("1111"); let output_dir = matches.value_of("output_dir").unwrap(); + let base_url = matches.value_of("base_url").unwrap(); console::info("Building site..."); - match cmd::serve(interface, port, output_dir, config_file) { + match cmd::serve(interface, port, output_dir, base_url, config_file) { Ok(()) => (), Err(e) => { console::unravel_errors("", &e);