From 5e141e6b8d73e2097fb895c1f1cc76e71699a29e Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 23 Nov 2019 06:44:34 -0500 Subject: move server address to configuration --- teleterm/src/cmd/web.rs | 5 ++++- teleterm/src/config.rs | 27 ++++++++++++++++++++--- teleterm/src/web.rs | 41 +++++++++++++++++++++++++++-------- teleterm/static/teleterm_web.js | 28 ++++++++++++------------ teleterm/static/teleterm_web_bg.wasm | Bin 703802 -> 703802 bytes 5 files changed, 74 insertions(+), 27 deletions(-) diff --git a/teleterm/src/cmd/web.rs b/teleterm/src/cmd/web.rs index 6f82815..37f2f70 100644 --- a/teleterm/src/cmd/web.rs +++ b/teleterm/src/cmd/web.rs @@ -17,7 +17,10 @@ impl crate::config::Config for Config { fn run( &self, ) -> Box + Send> { - Box::new(crate::web::Server::new(self.web.listen_address)) + Box::new(crate::web::Server::new( + self.web.listen_address, + self.web.server_address.clone(), + )) } } diff --git a/teleterm/src/config.rs b/teleterm/src/config.rs index 8e555d8..390ee4c 100644 --- a/teleterm/src/config.rs +++ b/teleterm/src/config.rs @@ -19,6 +19,7 @@ const MAX_FRAME_LENGTH_OPTION: &str = "max-frame-length"; const PLAY_AT_START_OPTION: &str = "play-at-start"; const PLAYBACK_RATIO_OPTION: &str = "playback-ratio"; const READ_TIMEOUT_OPTION: &str = "read-timeout-secs"; +const SERVER_ADDRESS_OPTION: &str = "server-address"; const TLS_IDENTITY_FILE_OPTION: &str = "tls-identity-file"; const TLS_OPTION: &str = "tls"; @@ -203,8 +204,7 @@ fn default_connect_address() -> (String, std::net::SocketAddr) { } // XXX this does a blocking dns lookup - should try to find an async version -// XXX shouldn't need to be pub -pub fn to_connect_address( +fn to_connect_address( address: &str, ) -> Result<(String, std::net::SocketAddr)> { let mut address_parts = address.split(':'); @@ -560,12 +560,20 @@ pub struct Web { default = "default_web_listen_address" )] pub listen_address: std::net::SocketAddr, + + #[serde( + deserialize_with = "connect_address", + default = "default_connect_address" + )] + pub server_address: (String, std::net::SocketAddr), } impl Web { pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> { let listen_address_help = - "Host and port to listen on (defaults to localhost:4144)"; + "Host and port to listen on (defaults to localhost:4145)"; + let server_address_help = + "Host and port of the teleterm server (defaults to localhost:4144)"; app.arg( clap::Arg::with_name(LISTEN_ADDRESS_OPTION) .long(LISTEN_ADDRESS_OPTION) @@ -573,7 +581,15 @@ impl Web { .value_name("HOST:PORT") .help(listen_address_help), ) + .arg( + clap::Arg::with_name(SERVER_ADDRESS_OPTION) + .long(SERVER_ADDRESS_OPTION) + .takes_value(true) + .value_name("HOST:PORT") + .help(server_address_help), + ) } + pub fn merge_args<'a>( &mut self, matches: &clap::ArgMatches<'a>, @@ -585,6 +601,10 @@ impl Web { .parse() .context(crate::error::ParseAddr)?; } + if matches.is_present(SERVER_ADDRESS_OPTION) { + let address = matches.value_of(SERVER_ADDRESS_OPTION).unwrap(); + self.server_address = to_connect_address(address)?; + } Ok(()) } } @@ -593,6 +613,7 @@ impl Default for Web { fn default() -> Self { Self { listen_address: default_web_listen_address(), + server_address: default_connect_address(), } } } diff --git a/teleterm/src/web.rs b/teleterm/src/web.rs index 40f23ce..5531356 100644 --- a/teleterm/src/web.rs +++ b/teleterm/src/web.rs @@ -19,6 +19,7 @@ struct WatchQueryParams { #[derive(Clone, serde::Serialize)] struct Config { title: String, + server_address: (String, std::net::SocketAddr), } pub struct Server { @@ -26,12 +27,19 @@ pub struct Server { } impl Server { - pub fn new(addr: T) -> Self { + pub fn new( + listen_address: std::net::SocketAddr, + server_address: (String, std::net::SocketAddr), + ) -> Self { let data = Config { title: "teleterm".to_string(), + server_address, }; Self { - server: Box::new(gotham::init_server(addr, router(&data))), + server: Box::new(gotham::init_server( + listen_address, + router(&data), + )), } } } @@ -81,11 +89,13 @@ fn router(data: &Config) -> impl gotham::handler::NewHandler { route .get("/teleterm.css") .to(serve_static("text/css", &view::TELETERM_CSS)); - route.get("/list").to(handle_list); + route + .get("/list") + .to_new_handler(serve_dynamic(data, handle_list)); route .get("/watch") .with_query_string_extractor::() - .to(handle_watch); + .to_new_handler(serve_dynamic(data, handle_watch)); }) } @@ -121,11 +131,25 @@ fn serve_template( } } +fn serve_dynamic( + data: &Config, + handler: fn( + &Config, + gotham::state::State, + ) -> (gotham::state::State, hyper::Response), +) -> impl gotham::handler::NewHandler { + let data = data.clone(); + move || { + let data = data.clone(); + Ok(move |state| handler(&data, state)) + } +} + fn handle_list( + config: &Config, state: gotham::state::State, ) -> (gotham::state::State, hyper::Response) { - let address = "127.0.0.1:4144"; - let (_, address) = crate::config::to_connect_address(address).unwrap(); + let (_, address) = config.server_address; let connector: crate::client::Connector<_> = Box::new(move || { Box::new( tokio::net::tcp::TcpStream::connect(&address) @@ -249,6 +273,7 @@ impl } fn handle_watch( + config: &Config, mut state: gotham::state::State, ) -> (gotham::state::State, hyper::Response) { let body = hyper::Body::take_from(&mut state); @@ -269,9 +294,7 @@ fn handle_watch( }; let query_params = WatchQueryParams::borrow_from(&state); - let address = "127.0.0.1:4144"; - let (_, address) = - crate::config::to_connect_address(address).unwrap(); + let (_, address) = config.server_address; let connector: crate::client::Connector<_> = Box::new(move || { Box::new( tokio::net::tcp::TcpStream::connect(&address) diff --git a/teleterm/static/teleterm_web.js b/teleterm/static/teleterm_web.js index e863f35..d5bddf8 100644 --- a/teleterm/static/teleterm_web.js +++ b/teleterm/static/teleterm_web.js @@ -914,14 +914,14 @@ function init(module) { const ret = real; return addHeapObject(ret); }; - imports.wbg.__wbindgen_closure_wrapper604 = function(arg0, arg1, arg2) { + imports.wbg.__wbindgen_closure_wrapper603 = function(arg0, arg1, arg2) { const state = { a: arg0, b: arg1, cnt: 1 }; const real = (arg0) => { state.cnt++; const a = state.a; state.a = 0; try { - return __wbg_elem_binding4(a, state.b, arg0); + return __wbg_elem_binding5(a, state.b, arg0); } finally { if (--state.cnt === 0) wasm.__wbg_function_table.get(32)(a, state.b); else state.a = a; @@ -932,14 +932,14 @@ function init(module) { const ret = real; return addHeapObject(ret); }; - imports.wbg.__wbindgen_closure_wrapper605 = function(arg0, arg1, arg2) { + imports.wbg.__wbindgen_closure_wrapper794 = function(arg0, arg1, arg2) { const state = { a: arg0, b: arg1, cnt: 1 }; const real = (arg0) => { state.cnt++; const a = state.a; state.a = 0; try { - return __wbg_elem_binding0(a, state.b, arg0); + return __wbg_elem_binding1(a, state.b, arg0); } finally { if (--state.cnt === 0) wasm.__wbg_function_table.get(32)(a, state.b); else state.a = a; @@ -950,14 +950,14 @@ function init(module) { const ret = real; return addHeapObject(ret); }; - imports.wbg.__wbindgen_closure_wrapper506 = function(arg0, arg1, arg2) { + imports.wbg.__wbindgen_closure_wrapper665 = function(arg0, arg1, arg2) { const state = { a: arg0, b: arg1, cnt: 1 }; - const real = () => { + const real = (arg0) => { state.cnt++; const a = state.a; state.a = 0; try { - return __wbg_elem_binding6(a, state.b, ); + return __wbg_elem_binding0(a, state.b, arg0); } finally { if (--state.cnt === 0) wasm.__wbg_function_table.get(32)(a, state.b); else state.a = a; @@ -968,14 +968,14 @@ function init(module) { const ret = real; return addHeapObject(ret); }; - imports.wbg.__wbindgen_closure_wrapper603 = function(arg0, arg1, arg2) { + imports.wbg.__wbindgen_closure_wrapper506 = function(arg0, arg1, arg2) { const state = { a: arg0, b: arg1, cnt: 1 }; - const real = (arg0) => { + const real = () => { state.cnt++; const a = state.a; state.a = 0; try { - return __wbg_elem_binding3(a, state.b, arg0); + return __wbg_elem_binding6(a, state.b, ); } finally { if (--state.cnt === 0) wasm.__wbg_function_table.get(32)(a, state.b); else state.a = a; @@ -986,14 +986,14 @@ function init(module) { const ret = real; return addHeapObject(ret); }; - imports.wbg.__wbindgen_closure_wrapper665 = function(arg0, arg1, arg2) { + imports.wbg.__wbindgen_closure_wrapper604 = function(arg0, arg1, arg2) { const state = { a: arg0, b: arg1, cnt: 1 }; const real = (arg0) => { state.cnt++; const a = state.a; state.a = 0; try { - return __wbg_elem_binding5(a, state.b, arg0); + return __wbg_elem_binding4(a, state.b, arg0); } finally { if (--state.cnt === 0) wasm.__wbg_function_table.get(32)(a, state.b); else state.a = a; @@ -1004,14 +1004,14 @@ function init(module) { const ret = real; return addHeapObject(ret); }; - imports.wbg.__wbindgen_closure_wrapper794 = function(arg0, arg1, arg2) { + imports.wbg.__wbindgen_closure_wrapper605 = function(arg0, arg1, arg2) { const state = { a: arg0, b: arg1, cnt: 1 }; const real = (arg0) => { state.cnt++; const a = state.a; state.a = 0; try { - return __wbg_elem_binding1(a, state.b, arg0); + return __wbg_elem_binding3(a, state.b, arg0); } finally { if (--state.cnt === 0) wasm.__wbg_function_table.get(32)(a, state.b); else state.a = a; diff --git a/teleterm/static/teleterm_web_bg.wasm b/teleterm/static/teleterm_web_bg.wasm index 87a4f8f..dc787ef 100644 Binary files a/teleterm/static/teleterm_web_bg.wasm and b/teleterm/static/teleterm_web_bg.wasm differ -- cgit v1.2.3-54-g00ecf