Skip to content

Commit

Permalink
Merge pull request #50 from lomirus/open-browser
Browse files Browse the repository at this point in the history
Add open browser argument
  • Loading branch information
lomirus authored Dec 22, 2023
2 parents ac2ac9b + 16cf9c9 commit 0319e2b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 13 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"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Arguments:
Options:
-H, --host <HOST> Set the listener host [default: 0.0.0.0]
-p, --port <PORT> Set the listener port [default: 0]
-o, --open Open the page in browser automatically
-h, --help Print help
-V, --version Print version
```
Expand Down
31 changes: 23 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
mod server;
mod watcher;

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

use axum::Router;
use notify::ReadDirectoryChangesWatcher;
use local_ip_address::local_ip;
use notify::RecommendedWatcher;
use notify_debouncer_full::{DebouncedEvent, Debouncer, FileIdMap};
use server::{create_listener, create_server};
use tokio::{
Expand All @@ -37,13 +38,13 @@ pub struct Listener {
tcp_listener: TcpListener,
router: Router,
root_path: PathBuf,
debouncer: Debouncer<ReadDirectoryChangesWatcher, FileIdMap>,
debouncer: Debouncer<RecommendedWatcher, FileIdMap>,
rx: Receiver<Result<Vec<DebouncedEvent>, Vec<notify::Error>>>,
}

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();
}
6 changes: 3 additions & 3 deletions src/watcher.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{path::PathBuf, time::Duration};

use notify::{Error, ReadDirectoryChangesWatcher, RecursiveMode, Watcher};
use notify::{Error, RecommendedWatcher, RecursiveMode, Watcher};
use notify_debouncer_full::{
new_debouncer, DebounceEventResult, DebouncedEvent, Debouncer, FileIdMap,
};
Expand All @@ -21,7 +21,7 @@ pub(crate) async fn create_watcher(
root: PathBuf,
) -> Result<
(
Debouncer<ReadDirectoryChangesWatcher, FileIdMap>,
Debouncer<RecommendedWatcher, FileIdMap>,
PathBuf,
Receiver<Result<Vec<DebouncedEvent>, Vec<Error>>>,
),
Expand Down Expand Up @@ -65,7 +65,7 @@ pub(crate) async fn create_watcher(

pub async fn watch(
root_path: PathBuf,
mut debouncer: Debouncer<ReadDirectoryChangesWatcher, FileIdMap>,
mut debouncer: Debouncer<RecommendedWatcher, FileIdMap>,
mut rx: Receiver<Result<Vec<DebouncedEvent>, Vec<Error>>>,
) {
debouncer
Expand Down

0 comments on commit 0319e2b

Please sign in to comment.