diff --git a/src/controller.rs b/src/controller.rs index cc67be87..618b16a9 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -32,8 +32,6 @@ pub struct FoxBox { websockets: Arc>>, } -pub const DEFAULT_HTTP_PORT: u16 = 3000; -const DEFAULT_WS_PORT: u16 = 4000; const DEFAULT_HOSTNAME: &'static str = "::"; // ipv6 default. const DEFAULT_DOMAIN: &'static str = ".local"; @@ -60,8 +58,8 @@ impl FoxBox { pub fn new(verbose: bool, hostname: Option, - http_port: Option, - ws_port: Option) -> Self { + http_port: u16, + ws_port: u16) -> Self { FoxBox { services: Arc::new(Mutex::new(HashMap::new())), @@ -70,8 +68,8 @@ impl FoxBox { hostname: hostname.map_or(DEFAULT_HOSTNAME.to_owned(), |name| { format!("{}{}", name, DEFAULT_DOMAIN) }), - http_port: http_port.unwrap_or(DEFAULT_HTTP_PORT), - ws_port: ws_port.unwrap_or(DEFAULT_WS_PORT) + http_port: http_port, + ws_port: ws_port } } } @@ -194,7 +192,7 @@ describe! controller { use stubs::service::ServiceStub; let service = ServiceStub; - let controller = FoxBox::new(false, Some("foxbox".to_owned()), None, None); + let controller = FoxBox::new(false, Some("foxbox".to_owned()), 1234, 5678); } describe! add_service { @@ -217,13 +215,13 @@ describe! controller { it "should create http root" { controller.add_service(Box::new(service)); assert_eq!(controller.get_http_root_for_service("1".to_string()), - "http://foxbox.local:3000/services/1/"); + "http://foxbox.local:1234/services/1/"); } it "should create ws root" { controller.add_service(Box::new(service)); assert_eq!(controller.get_ws_root_for_service("1".to_string()), - "ws://foxbox.local:4000/services/1/"); + "ws://foxbox.local:5678/services/1/"); } it "should return a json" { diff --git a/src/main.rs b/src/main.rs index 610c0539..18bb29e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -68,7 +68,7 @@ mod stubs { pub mod service; } -use controller::{ Controller, FoxBox, DEFAULT_HTTP_PORT }; +use controller::{ Controller, FoxBox }; use tunnel_controller:: { TunnelConfig, Tunnel }; use multicast_dns::host::HostManager; @@ -77,7 +77,7 @@ Usage: foxbox [-v] [-h] [-n ] [-p ] [-w ] [-r ] [-i Options: -v, --verbose Toggle verbose output. - -n, --name Set local hostname. [default: foxbox] + -n, --name Set local hostname. Linux only. Requires to be a member of the netdev group. -p, --port Set port to listen on for http connections. [default: 3000] -w, --wsport Set port to listen on for websocket. [default: 4000] -r, --register Change the url of the registration endpoint. [default: http://localhost:4242/register] @@ -86,10 +86,10 @@ Options: -h, --help Print this help menu. ", flag_name: Option, - flag_port: Option, - flag_wsport: Option, + flag_port: u16, + flag_wsport: u16, + flag_register: String, flag_iface: Option, - flag_register: Option, flag_tunnel: Option); /// Updates local host name with the provided host name string. If requested host name @@ -126,7 +126,7 @@ fn main() { // Start the tunnel. if let Some(host) = args.flag_tunnel { let mut tunnel = - Tunnel::new(TunnelConfig::new(args.flag_port.unwrap_or(DEFAULT_HTTP_PORT), host)); + Tunnel::new(TunnelConfig::new(args.flag_port, host)); tunnel.start().unwrap(); } @@ -146,10 +146,10 @@ describe! main { .decode().unwrap(); assert_eq!(args.flag_verbose, false); - assert_eq!(args.flag_name.unwrap(), "foxbox"); - assert_eq!(args.flag_port.unwrap(), 3000); - assert_eq!(args.flag_wsport.unwrap(), 4000); - assert_eq!(args.flag_register.unwrap(), "http://localhost:4242/register"); + assert_eq!(args.flag_name, None); + assert_eq!(args.flag_port, 3000); + assert_eq!(args.flag_wsport, 4000); + assert_eq!(args.flag_register, "http://localhost:4242/register"); assert_eq!(args.flag_iface, None); assert_eq!(args.flag_tunnel, None); assert_eq!(args.flag_help, false); @@ -170,9 +170,9 @@ describe! main { assert_eq!(args.flag_verbose, true); assert_eq!(args.flag_name.unwrap(), "foobar"); - assert_eq!(args.flag_port.unwrap(), 1234); - assert_eq!(args.flag_wsport.unwrap(), 4567); - assert_eq!(args.flag_register.unwrap(), "http://foo.bar:6868/register"); + assert_eq!(args.flag_port, 1234); + assert_eq!(args.flag_wsport, 4567); + assert_eq!(args.flag_register, "http://foo.bar:6868/register"); assert_eq!(args.flag_iface.unwrap(), "eth99"); assert_eq!(args.flag_tunnel.unwrap(), "tunnel.host"); } @@ -192,9 +192,9 @@ describe! main { assert_eq!(args.flag_verbose, true); assert_eq!(args.flag_name.unwrap(), "foobar"); - assert_eq!(args.flag_port.unwrap(), 1234); - assert_eq!(args.flag_wsport.unwrap(), 4567); - assert_eq!(args.flag_register.unwrap(), "http://foo.bar:6868/register"); + assert_eq!(args.flag_port, 1234); + assert_eq!(args.flag_wsport, 4567); + assert_eq!(args.flag_register, "http://foo.bar:6868/register"); assert_eq!(args.flag_iface.unwrap(), "eth99"); assert_eq!(args.flag_tunnel.unwrap(), "tunnel.host"); } diff --git a/src/registration.rs b/src/registration.rs index ebd8a2d7..a858727e 100644 --- a/src/registration.rs +++ b/src/registration.rs @@ -17,7 +17,6 @@ use std::io::Read; use std::time::Duration; use std::thread; -const DEFAULT_ENDPOINT: &'static str = "http://localhost:4242/register"; const REGISTRATION_INTERVAL: u32 = 1; // in minutes. pub struct Registrar; @@ -27,13 +26,8 @@ impl Registrar { Registrar } - pub fn start(&self, endpoint: Option, iface: Option) { - let url = match endpoint { - Some(u) => u, - _ => DEFAULT_ENDPOINT.to_owned() - }; - - info!("Starting registration with {}", url); + pub fn start(&self, endpoint_url: String, iface: Option) { + info!("Starting registration with {}", endpoint_url); let ip_addr = self.get_ip_addr(&iface); if ip_addr == None { // TODO: retry later, in case we're racing with the network @@ -42,7 +36,7 @@ impl Registrar { } info!("Got ip address: {}", ip_addr.clone().unwrap()); - let full_address = format!("{}?ip={}", url, ip_addr.unwrap()); + let full_address = format!("{}?ip={}", endpoint_url, ip_addr.unwrap()); // Spawn a thread to register every REGISTRATION_INTERVAL minutes. thread::Builder::new().name("Registrar".to_owned()) diff --git a/src/service_router.rs b/src/service_router.rs index f951df98..c9cd1d2d 100644 --- a/src/service_router.rs +++ b/src/service_router.rs @@ -142,7 +142,7 @@ describe! service_router { use iron_test::request; use mount::Mount; - let controller = FoxBox::new(false, Some("localhost".to_owned()), None, None); + let controller = FoxBox::new(false, Some("localhost".to_owned()), 1234, 5678); let service_router = create(controller.clone()); let mut mount = Mount::new(); @@ -171,7 +171,7 @@ describe! service_router { username: "username".to_owned(), password: Some("password".to_owned()) })); - let response = request::post("http://localhost:3000/users/login", + let response = request::post("http://localhost:1234/users/login", headers, "{}", &mount).unwrap(); @@ -190,7 +190,7 @@ describe! service_router { } it "should create list.json" { - let response = request::get("http://localhost:3000/list.json", + let response = request::get("http://localhost:1234/list.json", auth_header, &mount).unwrap(); @@ -202,7 +202,7 @@ describe! service_router { use controller::Controller; use stubs::service::ServiceStub; controller.add_service(Box::new(ServiceStub)); - let response = request::get("http://localhost:3000/1/a-command", + let response = request::get("http://localhost:1234/1/a-command", auth_header, &mount).unwrap(); @@ -211,7 +211,7 @@ describe! service_router { } it "should return an error if no service was found" { - let response = request::get("http://localhost:3000/unknown-id/a-command", + let response = request::get("http://localhost:1234/unknown-id/a-command", auth_header, &mount).unwrap(); @@ -229,7 +229,7 @@ describe! service_router { it "should get the appropriate CORS headers" { for endpoint in CORS::ENDPOINTS { let (_, path) = *endpoint; - let path = "http://localhost:3000/".to_owned() + + let path = "http://localhost:1234/".to_owned() + &(path.replace(":", "foo")); let response = request::options(&path, Headers::new(), &mount).unwrap(); let headers = &response.headers;