Don't hardcode the ws port

Closes #282
This commit is contained in:
Vincent Prouillet 2018-05-11 13:54:16 +02:00
parent 50a79c52f3
commit b892c07ed3
7 changed files with 26 additions and 9 deletions

View file

@ -4,6 +4,7 @@
## 0.4.0 (unreleased) ## 0.4.0 (unreleased)
- Fix `serve` not working with the config flag - 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) ## 0.3.4 (2018-06-22)

View file

@ -33,6 +33,7 @@ use errors::{Result, ResultExt};
use config::{Config, get_config}; use config::{Config, get_config};
use utils::fs::{create_file, copy_directory, create_directory, ensure_directory_exists}; use utils::fs::{create_file, copy_directory, create_directory, ensure_directory_exists};
use utils::templates::{render_template, rewrite_theme_paths}; 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 content::{Page, Section, populate_previous_and_next_pages, sort_pages};
use templates::{GUTENBERG_TERA, global_fns, render_redirect_template}; use templates::{GUTENBERG_TERA, global_fns, render_redirect_template};
use front_matter::{SortBy, InsertAnchor}; use front_matter::{SortBy, InsertAnchor};
@ -65,7 +66,8 @@ pub struct Site {
pub pages: HashMap<PathBuf, Page>, pub pages: HashMap<PathBuf, Page>,
pub sections: HashMap<PathBuf, Section>, pub sections: HashMap<PathBuf, Section>,
pub tera: Tera, pub tera: Tera,
live_reload: bool, // the live reload port to be used if there is one
pub live_reload: Option<u16>,
pub output_path: PathBuf, pub output_path: PathBuf,
pub static_path: PathBuf, pub static_path: PathBuf,
pub tags: Option<Taxonomy>, pub tags: Option<Taxonomy>,
@ -113,7 +115,7 @@ impl Site {
tera, tera,
pages: HashMap::new(), pages: HashMap::new(),
sections: HashMap::new(), sections: HashMap::new(),
live_reload: false, live_reload: None,
output_path: path.join("public"), output_path: path.join("public"),
static_path: path.join("static"), static_path: path.join("static"),
tags: None, tags: None,
@ -129,9 +131,8 @@ impl Site {
self.base_path.join("content").join("_index.md") self.base_path.join("content").join("_index.md")
} }
/// What the function name says
pub fn enable_live_reload(&mut self) { 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 /// 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 /// Inject live reload script tag if in live reload mode
fn inject_livereload(&self, html: String) -> String { fn inject_livereload(&self, html: String) -> String {
if self.live_reload { if let Some(port) = self.live_reload {
return html.replace( return html.replace(
"</body>", "</body>",
r#"<script src="/livereload.js?port=1112&mindelay=10"></script></body>"# &format!(r#"<script src="/livereload.js?port={}&mindelay=10"></script></body>"#, port)
); );
} }

View file

@ -197,7 +197,7 @@ fn can_build_site_with_live_reload() {
assert_eq!(file_exists!(public, "tags/index.html"), false); assert_eq!(file_exists!(public, "tags/index.html"), false);
// no live reload code // 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 // the summary anchor link has been created
assert!(file_contains!(public, "posts/python/index.html", r#"<a name="continue-reading"></a>"#)); assert!(file_contains!(public, "posts/python/index.html", r#"<a name="continue-reading"></a>"#));

View file

@ -9,3 +9,4 @@ extern crate walkdir;
pub mod fs; pub mod fs;
pub mod site; pub mod site;
pub mod templates; pub mod templates;
pub mod net;

View file

@ -0,0 +1,14 @@
use std::net::TcpListener;
pub fn get_available_port() -> Option<u16> {
(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,
}
}

View file

@ -27,7 +27,7 @@ pub fn render_template(name: &str, tera: &Tera, context: &Context, theme: &Optio
.map_err(|e| e.into()); .map_err(|e| e.into());
} }
if let &Some(ref t) = theme { if let Some(ref t) = *theme {
return tera return tera
.render(&format!("{}/templates/{}", t, name), context) .render(&format!("{}/templates/{}", t, name), context)
.map_err(|e| e.into()); .map_err(|e| e.into());

View file

@ -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 // Sass support is optional so don't make it an error to no have a sass folder
let _ = watcher.watch("sass/", RecursiveMode::Recursive); 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 // Start a webserver that serves the `output_dir` directory
let mut mount = Mount::new(); let mut mount = Mount::new();