From 55239ec4aa53e6301621c3007eb495da24bf551f Mon Sep 17 00:00:00 2001 From: James Date: Sat, 4 Aug 2018 00:54:03 +1000 Subject: [PATCH] Bind to both IPv4 and 6 --- src/main.rs | 33 ++++++++++++++++++++++++++++++--- src/rest.rs | 14 ++------------ src/tasks/mod.rs | 4 ++-- static/index.html | 5 +++-- 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index 61b474d..32d476f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,6 +47,11 @@ use nfd::Response; use rest::WebServer; +use std::net::ToSocketAddrs; + +use std::sync::Arc; +use std::sync::RwLock; + // TODO: Fetch this over a HTTP request? static RAW_CONFIG: &'static str = include_str!("../config.toml"); @@ -73,11 +78,33 @@ fn main() { InstallerFramework::new(config) }; - let server = WebServer::new(framework).unwrap(); + let addresses = "localhost:0" + .to_socket_addrs() + .expect("No localhost address found"); + + let mut servers = Vec::new(); + let mut http_address = None; + + let framework = Arc::new(RwLock::new(framework)); // Startup HTTP server for handling the web view - let http_address = format!("http://{}", server.get_addr()); - println!("Server: {:?}", http_address); + for address in addresses { + let server = WebServer::with_addr(framework.clone(), address).unwrap(); + + let addr = server.get_addr(); + println!("Server: {:?}", addr); + + http_address = Some(addr); + + servers.push(server); + } + + let http_address = match http_address { + Some(v) => v, + None => panic!("No HTTP address found"), + }; + + let http_address = format!("http://localhost:{}", http_address.port()); // Init the web view let size = (1024, 500); diff --git a/src/rest.rs b/src/rest.rs index a058fb9..adc6d96 100644 --- a/src/rest.rs +++ b/src/rest.rs @@ -47,25 +47,15 @@ impl WebServer { self.addr.clone() } - /// Creates a new web server, bound to a random port on localhost. - pub fn new(framework: InstallerFramework) -> Result { - WebServer::with_addr( - framework, - SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0), - ) - } - /// Creates a new web server with the specified address. - pub fn with_addr(framework: InstallerFramework, addr: SocketAddr) -> Result { + pub fn with_addr(framework: Arc>, addr: SocketAddr) -> Result { let (sender, receiver) = channel(); let handle = thread::spawn(move || { - let shared_framework = Arc::new(RwLock::new(framework)); - let server = Http::new() .bind(&addr, move || { Ok(WebService { - framework: shared_framework.clone(), + framework: framework.clone(), }) }) .unwrap(); diff --git a/src/tasks/mod.rs b/src/tasks/mod.rs index 1342dab..b5ceb93 100644 --- a/src/tasks/mod.rs +++ b/src/tasks/mod.rs @@ -27,7 +27,7 @@ pub enum TaskParamType { /// Downloaded contents of a file FileContents(Version, Vec), /// Tells the runtime to break parsing other dependencies - Break + Break, } /// A Task is a small, async task conforming to a fixed set of inputs/outputs. @@ -103,7 +103,7 @@ impl DependencyTree { // Check to see if we skip matching other dependencies let do_break = match &result { &TaskParamType::Break => true, - _ => false + _ => false, }; inputs.push(result); diff --git a/static/index.html b/static/index.html index e88eec3..3710d7f 100644 --- a/static/index.html +++ b/static/index.html @@ -225,9 +225,10 @@ app.modify_install = false; }, "uninstall": function() { + app.is_installing = true; app.confirm_uninstall = false; - this.select_packages = false; - this.is_installing = true; + app.modify_install = false; + app.select_packages = false; stream_ajax("/api/uninstall", function(line) { if (line.hasOwnProperty("Status")) {