diff --git a/Cargo.lock b/Cargo.lock index 682e602..b8c587b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1759,6 +1759,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "is-docker" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "928bae27f42bc99b60d9ac7334e3a21d10ad8f1835a4e12ec3ec0464765ed1b3" +dependencies = [ + "once_cell", +] + [[package]] name = "is-terminal" version = "0.4.9" @@ -1770,6 +1779,16 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "is-wsl" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "173609498df190136aa7dea1a91db051746d339e18476eed5ca40521f02d7aa5" +dependencies = [ + "is-docker", + "once_cell", +] + [[package]] name = "itertools" version = "0.10.5" @@ -2041,6 +2060,17 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" +[[package]] +name = "open" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfabf1927dce4d6fdf563d63328a0a506101ced3ec780ca2135747336c98cef8" +dependencies = [ + "is-wsl", + "libc", + "pathdiff", +] + [[package]] name = "option-ext" version = "0.2.0" @@ -2082,6 +2112,12 @@ version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" +[[package]] +name = "pathdiff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd" + [[package]] name = "pem-rfc7468" version = "0.7.0" @@ -2856,6 +2892,7 @@ dependencies = [ "humantime", "humantime-serde", "mime_guess", + "open", "rand", "rust-embed", "serde", diff --git a/Cargo.toml b/Cargo.toml index 8250aa6..8190686 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ gethostname = "0.4.3" humantime = "2.1.0" humantime-serde = "1.1.1" mime_guess = "2.0.4" +open = "5.0.0" rand = "0.8.5" rust-embed = { version = "6.8.1", features = ["interpolate-folder-path"] } serde = { version = "1.0.181", features = ["derive"] } diff --git a/src/args.rs b/src/args.rs index 6df348e..1e16422 100644 --- a/src/args.rs +++ b/src/args.rs @@ -14,6 +14,10 @@ pub struct ServerCommand { /// Path to the bench repo. #[arg(long, short)] pub bench_repo: Option, + + /// Open the UI in your browser. + #[arg(long, short)] + pub open: bool, } #[derive(Debug, clap::Parser)] diff --git a/src/main.rs b/src/main.rs index f6f74be..928475a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ mod server; mod shared; mod somehow; -use std::{io, process}; +use std::{io, process, time::Duration}; use clap::Parser; use tokio::{select, signal::unix::SignalKind}; @@ -82,6 +82,16 @@ async fn die_on_signal() -> io::Result<()> { process::exit(1); } +async fn open_in_browser(config: &Config) { + // Wait a bit to ensure the server is ready to serve requests. + tokio::time::sleep(Duration::from_millis(100)).await; + + let url = format!("http://{}{}", config.web_address, config.web_base); + if let Err(e) = open::that_detached(&url) { + error!("Error opening {url} in browser: {e:?}"); + } +} + async fn run() -> somehow::Result<()> { let args = Args::parse(); @@ -92,6 +102,10 @@ async fn run() -> somehow::Result<()> { match args.command { Command::Server(command) => { + if command.open { + tokio::task::spawn(open_in_browser(config)); + } + let server = Server::new(config, command).await?; select! {