Skip to content

Commit 30796d0

Browse files
fix port fallback on socket addr parsing
1 parent 1eba7f2 commit 30796d0

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/server.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::collections::HashMap;
55
use std::net::SocketAddr;
66
use std::path::PathBuf;
77

8+
use anyhow::Context as _;
89
use axum::error_handling::HandleError;
910
use axum::extract::ws::{self, WebSocket};
1011
use axum::extract::{Path, WebSocketUpgrade};
@@ -99,12 +100,14 @@ pub async fn run_server(options: Options, output: WasmBindgenOutput) -> Result<(
99100
.fallback_service(serve_dir)
100101
.layer(middleware_stack);
101102

102-
let mut address_string = options.address;
103-
if !address_string.contains(':') {
104-
address_string +=
105-
&(":".to_owned() + &pick_port::pick_free_port(1334, 10).unwrap_or(1334).to_string());
106-
}
107-
let addr: SocketAddr = address_string.parse().expect("Couldn't parse address");
103+
let addr: SocketAddr = options
104+
.address
105+
.parse()
106+
.or_else(|_| {
107+
format!("{}:{}", options.address, pick_port::pick_free_port(1334, 10).unwrap_or(1334))
108+
.parse()
109+
})
110+
.context("Error parsing WASM_SERVER_RUNNER_ADDRESS")?;
108111

109112
if options.https {
110113
let certificate = certificate::certificate()?;

0 commit comments

Comments
 (0)