Skip to content

Commit

Permalink
Add open browser argument
Browse files Browse the repository at this point in the history
  • Loading branch information
lomirus committed Dec 22, 2023
1 parent ac2ac9b commit 831013e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
37 changes: 37 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ tokio = { version = "1.35.0", features = ["full"] }
axum = { version = "0.7.2", features = ["ws"]}
futures = "0.3.29"
mime_guess = "2.0.3"
open = "5.0.1"

[dev-dependencies]
reqwest = "0.11.23"
27 changes: 21 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
mod server;
mod watcher;

use std::{error::Error, path::PathBuf};
use std::{error::Error, net::IpAddr, path::PathBuf};

use axum::Router;
use local_ip_address::local_ip;
use notify::ReadDirectoryChangesWatcher;
use notify_debouncer_full::{DebouncedEvent, Debouncer, FileIdMap};
use server::{create_listener, create_server};
Expand All @@ -43,7 +44,7 @@ pub struct Listener {

impl Listener {
/// Start live-server
///
///
/// ```
/// use live_server::listen;
///
Expand All @@ -52,9 +53,9 @@ impl Listener {
/// }
/// ```
pub async fn start(self) -> Result<(), Box<dyn Error>> {
ROOT.set(self.root_path.clone()).unwrap();
ROOT.set(self.root_path.clone())?;
let (tx, _) = broadcast::channel(16);
TX.set(tx).unwrap();
TX.set(tx)?;

let watcher_future = tokio::spawn(watcher::watch(self.root_path, self.debouncer, self.rx));
let server_future = tokio::spawn(server::serve(self.tcp_listener, self.router));
Expand All @@ -63,11 +64,25 @@ impl Listener {

Ok(())
}
}

pub fn link(&self) -> Result<String, Box<dyn Error>> {
let addr = self.tcp_listener.local_addr()?;
let port = addr.port();
let host = addr.ip();
let host = match host.is_unspecified() {
true => local_ip()?,
false => host,
};

Ok(match host {
IpAddr::V4(host) => format!("http://{host}:{port}"),
IpAddr::V6(host) => format!("http://[{host}]:{port}"),
})
}
}

/// Create live-server listener
///
///
/// ```
/// use live_server::listen;
///
Expand Down
18 changes: 16 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,30 @@ struct Args {
/// Set the listener port
#[clap(short, long, default_value = "0")]
port: u16,
/// Open the page in browser automatically
#[clap(short, long)]
open: bool,
}

#[tokio::main]
async fn main() {
let env = Env::new().default_filter_or("info");
env_logger::init_from_env(env);

let Args { host, port, root } = Args::parse();
let Args {
host,
port,
root,
open,
} = Args::parse();

let addr = format!("{}:{}", host, port);
let listener = listen(addr, root).await.unwrap();

listen(addr, root).await.unwrap().start().await.unwrap();
if open {
let link = listener.link().unwrap();
open::that(link).unwrap();
}

listener.start().await.unwrap();
}

0 comments on commit 831013e

Please sign in to comment.