Skip to content

Commit

Permalink
Parse interface as IpAddr, allow IPv6.
Browse files Browse the repository at this point in the history
  • Loading branch information
clarfonthey committed Dec 30, 2023
1 parent e5f3026 commit d99b98d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
3 changes: 2 additions & 1 deletion 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 Down
26 changes: 10 additions & 16 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,7 +321,7 @@ 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,
Expand All @@ -330,11 +330,11 @@ fn create_new_site(
include_drafts: bool,
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);

let base_url = if base_url == "/" {
String::from("/")
Expand Down Expand Up @@ -381,7 +381,7 @@ 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,
Expand All @@ -394,7 +394,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 +409,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 +462,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 +478,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
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn main() {
console::info("Building site...");
if let Err(e) = cmd::serve(
&root_dir,
&interface,
interface,
port,
output_dir.as_deref(),
force,
Expand Down

0 comments on commit d99b98d

Please sign in to comment.