diff options
author | Jesse Luehrs <doy@tozt.net> | 2019-11-14 16:30:47 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2019-11-14 16:30:47 -0500 |
commit | 69f9306c874f52ecee1f083b0a1075d3f4e175f4 (patch) | |
tree | 7980e7f1f61d55d4a05a7ce0324b894e3cd43132 | |
parent | 5310d6837408440815ee89db064aef08aff12e91 (diff) | |
download | teleterm-69f9306c874f52ecee1f083b0a1075d3f4e175f4.tar.gz teleterm-69f9306c874f52ecee1f083b0a1075d3f4e175f4.zip |
add configuration
-rw-r--r-- | src/cmd/web.rs | 19 | ||||
-rw-r--r-- | src/config.rs | 49 |
2 files changed, 61 insertions, 7 deletions
diff --git a/src/cmd/web.rs b/src/cmd/web.rs index 181ab74..a37468b 100644 --- a/src/cmd/web.rs +++ b/src/cmd/web.rs @@ -1,30 +1,35 @@ use crate::prelude::*; #[derive(serde::Deserialize, Debug, Default)] -pub struct Config {} +pub struct Config { + #[serde(default)] + web: crate::config::Web, +} impl crate::config::Config for Config { fn merge_args<'a>( &mut self, - _matches: &clap::ArgMatches<'a>, + matches: &clap::ArgMatches<'a>, ) -> Result<()> { - Ok(()) + self.web.merge_args(matches) } fn run( &self, ) -> Box<dyn futures::future::Future<Item = (), Error = Error> + Send> { - let addr = "127.0.0.1:4145"; Box::new( - gotham::init_server(addr, crate::web::router()) - .map_err(|_| unreachable!()), + gotham::init_server( + self.web.listen_address, + crate::web::router(), + ) + .map_err(|_| unreachable!()), ) } } pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> { - app.about("Run a teleterm web server") + crate::config::Web::cmd(app.about("Run a teleterm web server")) } pub fn config( diff --git a/src/config.rs b/src/config.rs index ceee9fc..20528c9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -24,6 +24,7 @@ const TLS_OPTION: &str = "tls"; const DEFAULT_LISTEN_ADDRESS: &str = "127.0.0.1:4144"; const DEFAULT_CONNECT_ADDRESS: &str = "127.0.0.1:4144"; +const DEFAULT_WEB_LISTEN_ADDRESS: &str = "127.0.0.1:4145"; const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(120); const DEFAULT_AUTH_TYPE: crate::protocol::AuthType = @@ -552,6 +553,54 @@ where } #[derive(serde::Deserialize, Debug)] +pub struct Web { + #[serde( + deserialize_with = "listen_address", + default = "default_web_listen_address" + )] + pub listen_address: 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)"; + app.arg( + clap::Arg::with_name(LISTEN_ADDRESS_OPTION) + .long(LISTEN_ADDRESS_OPTION) + .takes_value(true) + .value_name("HOST:PORT") + .help(listen_address_help), + ) + } + pub fn merge_args<'a>( + &mut self, + matches: &clap::ArgMatches<'a>, + ) -> Result<()> { + if matches.is_present(LISTEN_ADDRESS_OPTION) { + self.listen_address = matches + .value_of(LISTEN_ADDRESS_OPTION) + .unwrap() + .parse() + .context(crate::error::ParseAddr)?; + } + Ok(()) + } +} + +impl Default for Web { + fn default() -> Self { + Self { + listen_address: default_web_listen_address(), + } + } +} + +fn default_web_listen_address() -> std::net::SocketAddr { + to_listen_address(DEFAULT_WEB_LISTEN_ADDRESS).unwrap() +} + +#[derive(serde::Deserialize, Debug)] pub struct Command { #[serde(default = "default_command")] pub command: String, |