From aa33ba9297581257f0d7486a051b9537c3b04653 Mon Sep 17 00:00:00 2001 From: ltdk Date: Sat, 30 Dec 2023 16:20:16 -0500 Subject: [PATCH] Default base_url to socket address, instead of 127.0.0.1 --- src/cli.rs | 4 ++-- src/cmd/serve.rs | 15 ++++++++++++--- src/main.rs | 2 +- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 33a947f08e..1329edff4d 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -71,8 +71,8 @@ pub enum Command { force: bool, /// Changes the base_url - #[clap(short = 'u', long, default_value = "127.0.0.1")] - base_url: String, + #[clap(short = 'u', long)] + base_url: Option, /// Include drafts when loading the site #[clap(long)] diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index 510d5afdb4..74e42f8796 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -325,10 +325,10 @@ fn create_new_site( interface_port: u16, output_dir: Option<&Path>, force: bool, - base_url: &str, + base_url: Option<&str>, config_file: &Path, include_drafts: bool, - no_port_append: bool, + mut no_port_append: bool, ws_port: Option, ) -> Result<(Site, SocketAddr)> { SITE_CONTENT.write().unwrap().clear(); @@ -336,6 +336,15 @@ fn create_new_site( let mut site = Site::new(root_dir, config_file)?; let address = SocketAddr::new(interface, interface_port); + // if no base URL provided, use socket address + let base_url = base_url.map_or_else( + || { + no_port_append = true; + address.to_string() + }, + |u| u.to_string(), + ); + let base_url = if base_url == "/" { String::from("/") } else { @@ -385,7 +394,7 @@ pub fn serve( interface_port: u16, output_dir: Option<&Path>, force: bool, - base_url: &str, + base_url: Option<&str>, config_file: &Path, open: bool, include_drafts: bool, diff --git a/src/main.rs b/src/main.rs index c9c8d7faca..48e95b9693 100644 --- a/src/main.rs +++ b/src/main.rs @@ -106,7 +106,7 @@ fn main() { port, output_dir.as_deref(), force, - &base_url, + base_url.as_deref(), &config_file, open, drafts,