diff --git a/src/commands/mod.rs b/src/commands/mod.rs new file mode 100644 index 0000000..6193dd9 --- /dev/null +++ b/src/commands/mod.rs @@ -0,0 +1 @@ +pub mod relay; diff --git a/src/bin/relay_server.rs b/src/commands/relay.rs similarity index 85% rename from src/bin/relay_server.rs rename to src/commands/relay.rs index 8addd87..b944c89 100644 --- a/src/bin/relay_server.rs +++ b/src/commands/relay.rs @@ -1,8 +1,3 @@ -extern crate futures; -extern crate hyper; -extern crate webmetro; - -use std::env::args; use std::io::ErrorKind; use std::net::ToSocketAddrs; use std::sync::{ @@ -10,6 +5,7 @@ use std::sync::{ Mutex }; +use clap::{App, Arg, ArgMatches, SubCommand}; use futures::{ Future, Stream, @@ -112,8 +108,19 @@ impl Service for RelayServer { } } -pub fn main() { +pub fn args() -> App<'static, 'static> { + SubCommand::with_name("relay") + .about("Hosts an HTTP-based relay server") + .arg(Arg::with_name("listen") + .help("The address:port to listen to") + .required(true)) +} + +pub fn run(args: &ArgMatches) { let single_channel = Channel::new(); - let addr = args().nth(1).expect("Need binding address+port").to_socket_addrs().unwrap().next().unwrap(); + + let addr_str = args.value_of("listen").unwrap(); + let addr = addr_str.to_socket_addrs().expect("parsing bind address").next().expect("resolving bind address"); + Http::new().bind(&addr, move || Ok(RelayServer(single_channel.clone()))).unwrap().run().unwrap(); } diff --git a/src/main.rs b/src/main.rs index 1f44870..bfd104a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,12 @@ -#[macro_use] -extern crate clap; +#[macro_use] extern crate clap; +extern crate futures; +extern crate hyper; +extern crate webmetro; + +mod commands; use clap::{App, AppSettings}; +use commands::{relay}; fn main() { let args = App::new("webmetro") @@ -9,9 +14,11 @@ fn main() { .about("Utilities for broadcasting & relaying live WebM video/audio streams") .setting(AppSettings::SubcommandRequired) .setting(AppSettings::VersionlessSubcommands) + .subcommand(relay::args()) .get_matches(); match args.subcommand() { + ("relay", Some(sub_args)) => relay::run(sub_args), _ => {} } }