From c6ba724434152d2b7e9d7460f2a81ae83685d3e8 Mon Sep 17 00:00:00 2001 From: Emi Simpson Date: Sat, 30 Oct 2021 14:08:22 -0400 Subject: [PATCH] Allow specifying a socket address Resolves https://fem.mint.lgbt/Emi/PronounsToday/pulls/1#issuecomment-11 --- web/assets/default_config.yml | 3 +++ web/src/configuration.rs | 11 +++++++++++ web/src/main.rs | 7 ++++--- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/web/assets/default_config.yml b/web/assets/default_config.yml index 36f57da..7a01e9a 100644 --- a/web/assets/default_config.yml +++ b/web/assets/default_config.yml @@ -3,6 +3,9 @@ # The port the server should listen on port: 1312 +# The address the server should bind to +address: 0.0.0.0 + # A list of pronouns recognized by the server # # WARNING: When adding pronouns, only add pronouns to the bottom of the list, and do not diff --git a/web/src/configuration.rs b/web/src/configuration.rs index f06d6dd..558c68e 100644 --- a/web/src/configuration.rs +++ b/web/src/configuration.rs @@ -1,3 +1,5 @@ +use std::net::Ipv4Addr; +use std::net::IpAddr; use std::path::PathBuf; use pronouns_today::PronounList; use pronouns_today::UserPreferences; @@ -66,6 +68,10 @@ pub struct Run { /// the port to listen on pub port: Option, + #[argh(option)] + /// the address to bind to + pub address: Option, + #[argh(option)] /// default pronoun probabilites (formatted as a prefstring, like the ones in the /// used in the custom URLs, e.g. "acaqeawdym") @@ -122,11 +128,15 @@ pub struct Conf { /// The port for the server to bind to. Defaults to 1312 pub port: u16, + + /// The address to bind to. Defaults to 0.0.0.0 + pub address: IpAddr, } impl Conf { fn update_with(mut self, args: Run) -> Conf { self.port = args.port.unwrap_or(self.port); + self.address = args.address.unwrap_or(self.address); self.instance_settings.pronoun_list = args.pronouns.map(Into::into).unwrap_or(self.instance_settings.pronoun_list); self } @@ -137,6 +147,7 @@ impl Default for Conf { Conf { instance_settings: InstanceSettings::default(), port: 1312, + address: IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), } } } diff --git a/web/src/main.rs b/web/src/main.rs index 3f2c5b9..c0e1eac 100644 --- a/web/src/main.rs +++ b/web/src/main.rs @@ -5,6 +5,7 @@ pub mod configuration; use configuration::ConfigError; +use std::net::SocketAddr; use std::process::exit; use std::collections::HashMap; use std::fmt::{self, Display}; @@ -178,9 +179,9 @@ async fn main() -> std::io::Result<()> { async fn start_server(config: configuration::Conf) -> std::io::Result<()> { // Where we binding bois - let bind = format!("0.0.0.0:{}", config.port); + let socket_addr = SocketAddr::new(config.address, config.port); - println!("Starting pronouns-today-web on {}", &bind); + println!("Starting pronouns-today-web on {}", &socket_addr); HttpServer::new(move|| { let logger = Logger::default(); let app = App::new() @@ -204,7 +205,7 @@ async fn start_server(config: configuration::Conf) -> std::io::Result<()> { .service(resource("/{name}/{prefs}").to(handle_basic_request)) .default_service(web::to(not_found)) }) - .bind(&bind)? + .bind(&socket_addr)? .run() .await }