Skip to content

Commit e32f2ba

Browse files
committed
Auto merge of #4166 - Turbo87:server-url, r=locks
bin/server: Print HTTP server URL on startup This makes it easier to click on the URL instead of having to copy-paste the port. I'm aware that the code comment says "Do not change this line!", but I've adjusted the corresponding test code too... 😉
2 parents 4c1e6bd + e45cb50 commit e32f2ba

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/bin/server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
100100

101101
// When the user configures PORT=0 the operative system will allocate a random unused port.
102102
// This fetches that random port and uses it to display the "listening on port" message later.
103-
let actual_port = server.local_addr().port();
103+
let addr = server.local_addr();
104104

105105
let server = server.with_graceful_shutdown(async move {
106106
// Wait for either signal
@@ -116,7 +116,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
116116

117117
// Do not change this line! Removing the line or changing its contents in any way will break
118118
// the test suite :)
119-
println!("listening on port {}", actual_port);
119+
println!("Listening at http://{}", addr);
120120

121121
// Creating this file tells heroku to tell nginx that the application is ready
122122
// to receive traffic.

src/tests/server_binary.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::io::{BufRead, BufReader, Read};
99
use std::process::{Child, Command, Stdio};
1010
use std::sync::{mpsc::Sender, Arc};
1111
use std::time::Duration;
12+
use url::Url;
1213

1314
const SERVER_BOOT_TIMEOUT_SECONDS: u64 = 30;
1415

@@ -195,9 +196,12 @@ where
195196

196197
// If we expect the port number to be logged into this stream, look for it and send it
197198
// over the channel as soon as it's found.
198-
if let Some(port) = &port_send {
199-
if let Some(port_str) = line.strip_prefix("listening on port ") {
200-
port.send(port_str.into())
199+
if let Some(port_send) = &port_send {
200+
if let Some(url) = line.strip_prefix("Listening at ") {
201+
let url = Url::parse(url).unwrap();
202+
let port = url.port().unwrap();
203+
port_send
204+
.send(port.to_string())
201205
.expect("failed to send the port to the test thread")
202206
}
203207
}

0 commit comments

Comments
 (0)