Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow IPv6 serve, default base_url to listen interface instead of 127.0.0.1 #2395

Merged
merged 2 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::net::IpAddr;
use std::path::PathBuf;

use clap::{Parser, Subcommand};
Expand Down Expand Up @@ -54,7 +55,7 @@ pub enum Command {
Serve {
/// Interface to bind on
#[clap(short = 'i', long, default_value = "127.0.0.1")]
interface: String,
interface: IpAddr,

/// Which port to use
#[clap(short = 'p', long, default_value_t = 1111)]
Expand All @@ -70,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<String>,

/// Include drafts when loading the site
#[clap(long)]
Expand Down
41 changes: 22 additions & 19 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use std::cell::Cell;
use std::fs::read_dir;
use std::future::IntoFuture;
use std::net::{SocketAddrV4, TcpListener};
use std::net::{IpAddr, SocketAddr, TcpListener};
use std::path::{Path, PathBuf, MAIN_SEPARATOR};
use std::sync::mpsc::channel;
use std::sync::Mutex;
Expand Down Expand Up @@ -321,20 +321,29 @@ fn rebuild_done_handling(broadcaster: &Sender, res: Result<()>, reload_path: &st
#[allow(clippy::too_many_arguments)]
fn create_new_site(
root_dir: &Path,
interface: &str,
interface: IpAddr,
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<u16>,
) -> Result<(Site, String)> {
) -> Result<(Site, SocketAddr)> {
SITE_CONTENT.write().unwrap().clear();

let mut site = Site::new(root_dir, config_file)?;
let address = format!("{}:{}", interface, interface_port);
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("/")
Expand Down Expand Up @@ -381,11 +390,11 @@ fn create_new_site(
#[allow(clippy::too_many_arguments)]
pub fn serve(
root_dir: &Path,
interface: &str,
interface: IpAddr,
interface_port: u16,
output_dir: Option<&Path>,
force: bool,
base_url: &str,
base_url: Option<&str>,
config_file: &Path,
open: bool,
include_drafts: bool,
Expand All @@ -394,7 +403,7 @@ pub fn serve(
utc_offset: UtcOffset,
) -> Result<()> {
let start = Instant::now();
let (mut site, address) = create_new_site(
let (mut site, bind_address) = create_new_site(
root_dir,
interface,
interface_port,
Expand All @@ -409,12 +418,8 @@ pub fn serve(
messages::report_elapsed_time(start);

// Stop right there if we can't bind to the address
let bind_address: SocketAddrV4 = match address.parse() {
Ok(a) => a,
Err(_) => return Err(anyhow!("Invalid address: {}.", address)),
};
if (TcpListener::bind(bind_address)).is_err() {
return Err(anyhow!("Cannot start server on address {}.", address));
return Err(anyhow!("Cannot start server on address {}.", bind_address));
}

let config_path = PathBuf::from(config_file);
Expand Down Expand Up @@ -466,8 +471,6 @@ pub fn serve(
let static_root = output_path.clone();
let broadcaster = {
thread::spawn(move || {
let addr = address.parse().unwrap();

let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
Expand All @@ -484,11 +487,11 @@ pub fn serve(
}
});

let server = Server::bind(&addr).serve(make_service);
let server = Server::bind(&bind_address).serve(make_service);

println!("Web server is available at http://{}\n", &address);
println!("Web server is available at http://{}\n", bind_address);
if open {
if let Err(err) = open::that(format!("http://{}", &address)) {
if let Err(err) = open::that(format!("http://{}", bind_address)) {
eprintln!("Failed to open URL in your browser: {}", err);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ fn main() {
console::info("Building site...");
if let Err(e) = cmd::serve(
&root_dir,
&interface,
interface,
port,
output_dir.as_deref(),
force,
&base_url,
base_url.as_deref(),
&config_file,
open,
drafts,
Expand Down