Skip to content

Commit

Permalink
Fixup to e3b2201: use that_detached to open browser
Browse files Browse the repository at this point in the history
`open::that` can block, for example when `firefox` is the default browser
on Linux and no instance of `firefox` is running.
  • Loading branch information
ilyagr committed Mar 2, 2024
1 parent 43903d3 commit e0cb9d1
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions examples/poem/local-server-with-browser/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,16 @@ async fn main() -> Result<(), std::io::Error> {
// Now that the acceptor exists, the browser should be able to connect
eprintln!("Listening at {hostname}:{port}.");
let http_address = format!("http://{hostname}:{port}/");
eprint!("Trying to launch a browser at {http_address}...");
match open::that(&http_address) {
Ok(()) => eprintln!(" Success!"),
Err(err) => eprintln!("\nFailed to launch a browser: {err}"),
eprintln!("Trying to launch a browser at {http_address}...");
// We use `open::that_detached` so that launching, for example, a new
// instance of firefox on Linux does not block. This will report success
// even if the browser exits with a non-zero error code.
//
// You can alternatively consider using `tokio::spawn_blocking` and
// either `open::that` or the https://docs.rs/webbrowser crate.
match open::that_detached(&http_address) {
Ok(()) => { /* Ok() doesn't mean much with `that_detached`. */ }
Err(err) => eprintln!("Failed to launch a browser: {err}"),
}

Server::new_with_acceptor(acceptor).run(app).await?;
Expand Down

0 comments on commit e0cb9d1

Please sign in to comment.