Separate interface and base url for gutenberg serve (#223)

Separate interface and base_url for serve

You can now have a different base_url and listening interface.
The `gutenberg serve` cmd now accepts `-u`/`--base-url`, but it defaults to `127.0.0.1`.
This commit is contained in:
Andrew Zah 2018-02-02 11:18:07 -05:00 committed by Vincent Prouillet
parent 4d8647279c
commit f5ced05926
4 changed files with 20 additions and 8 deletions

View file

@ -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 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`). 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 ```bash
$ gutenberg serve $ gutenberg serve
$ gutenberg serve --port 2000 $ gutenberg serve --port 2000
$ gutenberg serve --interface 0.0.0.0 $ gutenberg serve --interface 0.0.0.0
$ gutenberg serve --interface 0.0.0.0 --port 2000 $ 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 $ gutenberg serve --interface 0.0.0.0 --port 2000 --output-dir www/public
``` ```

View file

@ -54,6 +54,12 @@ pub fn build_cli() -> App<'static, 'static> {
.default_value("public") .default_value("public")
.takes_value(true) .takes_value(true)
.help("Outputs the generated site in the given path"), .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"),
]), ]),
]) ])
} }

View file

@ -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 mut site = Site::new(env::current_dir().unwrap(), config_file)?;
let base_address = format!("{}:{}", base_url, port);
let address = format!("{}:{}", interface, 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('/') { site.config.base_url = if site.config.base_url.ends_with('/') {
format!("http://{}/", address) format!("http://{}/", base_address)
} else { } else {
format!("http://{}", address) format!("http://{}", base_address)
}; };
site.set_output_path(output_dir); site.set_output_path(output_dir);
site.load()?; site.load()?;
site.enable_live_reload(); site.enable_live_reload();
@ -99,9 +101,9 @@ fn create_new_site(interface: &str, port: &str, output_dir: &str, config_file: &
Ok((site, address)) 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 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); console::report_elapsed_time(start);
// Setup watchers // Setup watchers
@ -214,7 +216,7 @@ pub fn serve(interface: &str, port: &str, output_dir: &str, config_file: &str) -
}, },
(ChangeKind::Config, _) => { (ChangeKind::Config, _) => {
console::info(&format!("-> Config changed. The whole site will be reloaded. The browser needs to be refreshed to make the changes visible.")); 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); console::report_elapsed_time(start);

View file

@ -57,8 +57,9 @@ fn main() {
let interface = matches.value_of("interface").unwrap_or("127.0.0.1"); let interface = matches.value_of("interface").unwrap_or("127.0.0.1");
let port = matches.value_of("port").unwrap_or("1111"); let port = matches.value_of("port").unwrap_or("1111");
let output_dir = matches.value_of("output_dir").unwrap(); let output_dir = matches.value_of("output_dir").unwrap();
let base_url = matches.value_of("base_url").unwrap();
console::info("Building site..."); 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(()) => (), Ok(()) => (),
Err(e) => { Err(e) => {
console::unravel_errors("", &e); console::unravel_errors("", &e);