Browse Source

Moved argument parsing into module

* Port was redefined as i16 as that is the valid range for ports
* argument parsing is cleaned up and moved into args module
* correctly prefer arguments over config if present
develop
Drew Short 6 years ago
parent
commit
f95872126d
  1. 56
      src/args.rs
  2. 2
      src/config/model.rs
  3. 31
      src/main.rs

56
src/args.rs

@ -0,0 +1,56 @@
use std::str::FromStr;
use clap::ArgMatches;
use config::model::Config;
fn get_config_for_string(arg_value: Option<&str>, config_value: Option<String>, default_value: &str) -> String {
String::from(match arg_value {
Some(v) => v,
None => match c.server.host {
Some(host) => host,
None => default_host,
}
})
}
fn get_config_for_number<T: FromStr>(arg_value: Option<&str>, config_value: Option<T>, default_value: T) -> T {
match arg_value {
Some(v) => v.parse::<T>().unwrap_or(default_value),
None => match config_value {
Some(v) => v,
None => default_value
}
}
}
fn get_number<T: FromStr>(arg_value: Option<&str>, default_value: T) -> T {
match arg_value {
Some(value) => port.parse::<T>().unwrap_or(default_port),
None => default_port
}
}
pub fn get_host(args: &ArgMatches, config: &Option<Config>, default_host: &str) -> String {
let arg_value = args.value_of("host");
match config {
Some(c) => get_config_for_string(arg_value, c.server.host, default_host),
None => String::from(arg_value.unwrap_or(default_host))
}.clone()
}
pub fn get_port(args: &ArgMatches, config: &Option<Config>, default_port: i16) -> i16 {
let arg_value = args.value_of("port");
match config {
Some(c) => get_config_for_number(arg_value, c.server.port, default_port),
None => get_number(arg_value, default_port)
}
}
pub fn get_workers(args: &ArgMatches, config: &Option<Config>, default_workers: usize) -> usize {
let arg_value = args.value_of("workers");
match config {
Some(c) => get_config_for_number(arg_value, c.server.workers, default_workers),
None => get_number(args_value, default_workers)
}
}

2
src/config/model.rs

@ -1,7 +1,7 @@
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct ServerConfig { pub struct ServerConfig {
pub host: Option<String>, pub host: Option<String>,
pub port: Option<i32>,
pub port: Option<i16>,
pub workers: Option<usize>, pub workers: Option<usize>,
} }

31
src/main.rs

@ -15,12 +15,14 @@ extern crate serde_yaml;
use clap::{App, Arg}; use clap::{App, Arg};
mod server;
mod args;
mod config; mod config;
mod server;
const VERSION: &'static str = env!("CARGO_PKG_VERSION"); const VERSION: &'static str = env!("CARGO_PKG_VERSION");
const DEFAULT_HOST: &'static str = "localhost"; const DEFAULT_HOST: &'static str = "localhost";
const DEFAULT_PORT: i32 = 8080;
const DEFAULT_PORT: i16 = 8080;
lazy_static! { lazy_static! {
static ref DEFAULT_PORT_STR: String = String::from(DEFAULT_PORT.to_string()); static ref DEFAULT_PORT_STR: String = String::from(DEFAULT_PORT.to_string());
@ -83,28 +85,9 @@ fn main() {
} }
}; };
let host = String::from(match &config {
Some(c) => match &c.server.host {
Some(host) => host,
None => DEFAULT_HOST,
}
None => args.value_of("host").unwrap_or(DEFAULT_HOST)
}.clone());
let port = match &config {
Some(c) => c.server.port.unwrap_or(DEFAULT_PORT),
None => args
.value_of("port")
.unwrap()
.parse::<i32>()
.unwrap_or(DEFAULT_PORT)
};
let workers = match &config {
Some(c) => c.server.workers.unwrap_or(*DEFAULT_WORKERS),
None => match args.value_of("workers") {
Some(count) => count.parse::<usize>().unwrap_or(*DEFAULT_WORKERS),
None => num_cpus::get(),
}
};
let host = args::get_host(&args, &config, DEFAULT_HOST);
let port = args::get_port(&args, &config, DEFAULT_PORT);
let workers = args::get_workers(&args, &config, *DEFAULT_WORKERS);
info!( info!(
"Starting server on {}:{} with workers={} and config {}", "Starting server on {}:{} with workers={} and config {}",

Loading…
Cancel
Save