Allow specifying a socket address

Resolves #1 (comment)
This commit is contained in:
Emi Simpson 2021-10-30 14:08:22 -04:00
parent 6d087bd155
commit c6ba724434
Signed by: Emi
GPG Key ID: A12F2C2FFDC3D847
3 changed files with 18 additions and 3 deletions

View File

@ -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

View File

@ -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<u16>,
#[argh(option)]
/// the address to bind to
pub address: Option<IpAddr>,
#[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)),
}
}
}

View File

@ -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
}