Skip to content

Commit

Permalink
main: Remove unecessary Options in docopt
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanLorenzo committed Mar 2, 2016
1 parent e4b2814 commit 85ca6d9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 40 deletions.
16 changes: 7 additions & 9 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ pub struct FoxBox {
websockets: Arc<Mutex<HashMap<ws::util::Token, ws::Sender>>>,
}

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";

Expand All @@ -60,8 +58,8 @@ impl FoxBox {

pub fn new(verbose: bool,
hostname: Option<String>,
http_port: Option<u16>,
ws_port: Option<u16>) -> Self {
http_port: u16,
ws_port: u16) -> Self {

FoxBox {
services: Arc::new(Mutex::new(HashMap::new())),
Expand All @@ -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
}
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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" {
Expand Down
32 changes: 16 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -77,7 +77,7 @@ Usage: foxbox [-v] [-h] [-n <hostname>] [-p <port>] [-w <wsport>] [-r <url>] [-i
Options:
-v, --verbose Toggle verbose output.
-n, --name <hostname> Set local hostname. [default: foxbox]
-n, --name <hostname> Set local hostname. Linux only. Requires to be a member of the netdev group.
-p, --port <port> Set port to listen on for http connections. [default: 3000]
-w, --wsport <wsport> Set port to listen on for websocket. [default: 4000]
-r, --register <url> Change the url of the registration endpoint. [default: http://localhost:4242/register]
Expand All @@ -86,10 +86,10 @@ Options:
-h, --help Print this help menu.
",
flag_name: Option<String>,
flag_port: Option<u16>,
flag_wsport: Option<u16>,
flag_port: u16,
flag_wsport: u16,
flag_register: String,
flag_iface: Option<String>,
flag_register: Option<String>,
flag_tunnel: Option<String>);

/// Updates local host name with the provided host name string. If requested host name
Expand Down Expand Up @@ -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();
}

Expand All @@ -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);
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand Down
12 changes: 3 additions & 9 deletions src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,13 +26,8 @@ impl Registrar {
Registrar
}

pub fn start(&self, endpoint: Option<String>, iface: Option<String>) {
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<String>) {
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
Expand All @@ -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())
Expand Down
12 changes: 6 additions & 6 deletions src/service_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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;
Expand Down

0 comments on commit 85ca6d9

Please sign in to comment.