1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
//! Launch a local network server with live reload feature for static pages.
//!
//! ## Create live server
//! ```
//! use live_server::listen;
//! listen("127.0.0.1", 8080, "./").await.unwrap();
//! ```
//!
//! ## Enable logs (Optional)
//! ```rust
//! env_logger::init();
//! ```
mod server;
mod watcher;
use std::{collections::HashMap, sync::Arc};
use async_std::{path::PathBuf, sync::Mutex, task};
/// Watch the directory and create a static server.
/// ```
/// use live_server::listen;
/// listen("127.0.0.1", 8080, "./").await.unwrap();
/// ```
pub async fn listen<R: Into<PathBuf>>(
host: &str,
port: u16,
root: R,
) -> Result<(), std::io::Error> {
let connections1 = Arc::new(Mutex::new(HashMap::new()));
let connections2 = Arc::clone(&connections1);
let root1: PathBuf = root.into();
let root2: PathBuf = root1.clone();
task::spawn(async move { watcher::watch(root1, &connections1).await });
server::serve(host, port, root2, connections2).await
}