Allow configuration of bind address

This commit is contained in:
Magnus Hoff 2017-10-30 14:59:39 +01:00
parent b6d3cbf2c6
commit a6f707b911
2 changed files with 20 additions and 7 deletions

View file

@ -50,11 +50,12 @@ Command line arguments
-V, --version Prints version information -V, --version Prints version information
OPTIONS: OPTIONS:
-p, --port <port> Sets the listening port -a, --address <address> Sets the TCP address to bind to. Defaults to 127.0.0.1
-p, --port <port> Sets the listening port. Defaults to 8080
ARGS: ARGS:
<DATABASE> Sets the database file to use <DATABASE> Sets the database file to use
Sausagewiki will create an SQLite database file with the filename given in the Sausagewiki will create an SQLite database file with the filename given in the
`DATABASE` parameter and open an HTTP server bound to `127.0.0.1` and the given `DATABASE` parameter and open an HTTP server bound to the configured address,
port number. The default port number is 8080. `<address>:<port>`.

View file

@ -22,7 +22,7 @@ extern crate serde_urlencoded;
extern crate slug; extern crate slug;
extern crate titlecase; extern crate titlecase;
use std::net::SocketAddr; use std::net::{IpAddr, SocketAddr};
mod assets; mod assets;
mod db; mod db;
@ -38,6 +38,7 @@ mod wiki_lookup;
const DATABASE: &str = "DATABASE"; const DATABASE: &str = "DATABASE";
const TRUST_IDENTITY: &str = "trust-identity"; const TRUST_IDENTITY: &str = "trust-identity";
const ADDRESS: &str = "address";
const PORT: &str = "port"; const PORT: &str = "port";
fn args<'a>() -> clap::ArgMatches<'a> { fn args<'a>() -> clap::ArgMatches<'a> {
@ -49,12 +50,21 @@ fn args<'a>() -> clap::ArgMatches<'a> {
.help("Sets the database file to use") .help("Sets the database file to use")
.required(true)) .required(true))
.arg(Arg::with_name(PORT) .arg(Arg::with_name(PORT)
.help("Sets the listening port") .help("Sets the listening port. Defaults to 8080")
.short("p") .short("p")
.long(PORT) .long(PORT)
.validator(|x| match x.parse::<u16>() { .validator(|x| match x.parse::<u16>() {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(_) => Err("Must be an integer in the range [0, 65535]".to_owned()) Err(_) => Err("Must be an integer in the range [0, 65535]".into())
})
.takes_value(true))
.arg(Arg::with_name(ADDRESS)
.help("Sets the TCP address to bind to. Defaults to 127.0.0.1")
.short("a")
.long(ADDRESS)
.validator(|x| match x.parse::<IpAddr>() {
Ok(_) => Ok(()),
Err(_) => Err("Must be a valid IP address".into())
}) })
.takes_value(true)) .takes_value(true))
.arg(Arg::with_name(TRUST_IDENTITY) .arg(Arg::with_name(TRUST_IDENTITY)
@ -69,7 +79,9 @@ fn core_main() -> Result<(), Box<std::error::Error>> {
let args = args(); let args = args();
let db_file = args.value_of(DATABASE).expect("Guaranteed by clap").to_owned(); let db_file = args.value_of(DATABASE).expect("Guaranteed by clap").to_owned();
let bind_host = "127.0.0.1".parse().unwrap(); let bind_host = args.value_of(ADDRESS)
.map(|p| p.parse().expect("Guaranteed by validator"))
.unwrap_or_else(|| "127.0.0.1".parse().unwrap());
let bind_port = args.value_of(PORT) let bind_port = args.value_of(PORT)
.map(|p| p.parse().expect("Guaranteed by validator")) .map(|p| p.parse().expect("Guaranteed by validator"))
.unwrap_or(8080); .unwrap_or(8080);