diff --git a/CHANGELOG.md b/CHANGELOG.md index aab2ba8..7586449 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ## 0.4.0 (unreleased) - Fix `serve` not working with the config flag +- Websocket port on `live` will not get the first available port instead of a fixed one ## 0.3.4 (2018-06-22) diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index d8e434d..97968aa 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -33,6 +33,7 @@ use errors::{Result, ResultExt}; use config::{Config, get_config}; use utils::fs::{create_file, copy_directory, create_directory, ensure_directory_exists}; use utils::templates::{render_template, rewrite_theme_paths}; +use utils::net::get_available_port; use content::{Page, Section, populate_previous_and_next_pages, sort_pages}; use templates::{GUTENBERG_TERA, global_fns, render_redirect_template}; use front_matter::{SortBy, InsertAnchor}; @@ -65,7 +66,8 @@ pub struct Site { pub pages: HashMap, pub sections: HashMap, pub tera: Tera, - live_reload: bool, + // the live reload port to be used if there is one + pub live_reload: Option, pub output_path: PathBuf, pub static_path: PathBuf, pub tags: Option, @@ -113,7 +115,7 @@ impl Site { tera, pages: HashMap::new(), sections: HashMap::new(), - live_reload: false, + live_reload: None, output_path: path.join("public"), static_path: path.join("static"), tags: None, @@ -129,9 +131,8 @@ impl Site { self.base_path.join("content").join("_index.md") } - /// What the function name says pub fn enable_live_reload(&mut self) { - self.live_reload = true; + self.live_reload = get_available_port(); } /// Get all the orphan (== without section) pages in the site @@ -413,10 +414,10 @@ impl Site { /// Inject live reload script tag if in live reload mode fn inject_livereload(&self, html: String) -> String { - if self.live_reload { + if let Some(port) = self.live_reload { return html.replace( "", - r#""# + &format!(r#""#, port) ); } diff --git a/components/site/tests/site.rs b/components/site/tests/site.rs index 8c78d15..db2960e 100644 --- a/components/site/tests/site.rs +++ b/components/site/tests/site.rs @@ -197,7 +197,7 @@ fn can_build_site_with_live_reload() { assert_eq!(file_exists!(public, "tags/index.html"), false); // no live reload code - assert!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10")); + assert!(file_contains!(public, "index.html", "/livereload.js")); // the summary anchor link has been created assert!(file_contains!(public, "posts/python/index.html", r#""#)); diff --git a/components/utils/src/lib.rs b/components/utils/src/lib.rs index cddcb6f..a1fdb17 100644 --- a/components/utils/src/lib.rs +++ b/components/utils/src/lib.rs @@ -9,3 +9,4 @@ extern crate walkdir; pub mod fs; pub mod site; pub mod templates; +pub mod net; diff --git a/components/utils/src/net.rs b/components/utils/src/net.rs new file mode 100644 index 0000000..29c9376 --- /dev/null +++ b/components/utils/src/net.rs @@ -0,0 +1,14 @@ +use std::net::TcpListener; + + +pub fn get_available_port() -> Option { + (1000..9000) + .find(|port| port_is_available(*port)) +} + +fn port_is_available(port: u16) -> bool { + match TcpListener::bind(("127.0.0.1", port)) { + Ok(_) => true, + Err(_) => false, + } +} diff --git a/components/utils/src/templates.rs b/components/utils/src/templates.rs index e4270be..c7432ab 100644 --- a/components/utils/src/templates.rs +++ b/components/utils/src/templates.rs @@ -27,7 +27,7 @@ pub fn render_template(name: &str, tera: &Tera, context: &Context, theme: &Optio .map_err(|e| e.into()); } - if let &Some(ref t) = theme { + if let Some(ref t) = *theme { return tera .render(&format!("{}/templates/{}", t, name), context) .map_err(|e| e.into()); diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index 9a96459..20fece1 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -127,7 +127,7 @@ pub fn serve(interface: &str, port: &str, output_dir: &str, base_url: &str, conf // Sass support is optional so don't make it an error to no have a sass folder let _ = watcher.watch("sass/", RecursiveMode::Recursive); - let ws_address = format!("{}:{}", interface, "1112"); + let ws_address = format!("{}:{}", interface, site.live_reload.unwrap()); // Start a webserver that serves the `output_dir` directory let mut mount = Mount::new();