Browse Source

Dynamic defaults for workers and config setup from file or commandline

develop
Drew Short 6 years ago
parent
commit
85bd3b539b
  1. 32
      Cargo.lock
  2. 1
      Cargo.toml
  3. 16
      src/config.rs
  4. 51
      src/main.rs

32
Cargo.lock

@ -75,7 +75,7 @@ dependencies = [
"http 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
"httparse 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazycell 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"mime 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
@ -349,7 +349,7 @@ dependencies = [
"arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
"cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam-utils 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -362,7 +362,7 @@ dependencies = [
"arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
"cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam-utils 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -681,11 +681,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lazy_static"
version = "1.1.0"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"version_check 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazycell"
@ -832,7 +829,7 @@ name = "native-tls"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"openssl 0.10.15 (registry+https://github.com/rust-lang/crates.io-index)",
@ -875,7 +872,7 @@ dependencies = [
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
"openssl-sys 0.9.39 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -1155,7 +1152,7 @@ version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
"untrusted 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -1170,6 +1167,7 @@ dependencies = [
"cloudflare 0.1.0 (git+ssh://git@gitlab.sothr.com:9141/github/cloudflare-rs.git?branch=develop)",
"env_logger 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1205,7 +1203,7 @@ name = "schannel"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -1418,7 +1416,7 @@ name = "thread_local"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -1505,7 +1503,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"crossbeam-utils 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1615,7 +1613,7 @@ dependencies = [
"failure 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
"idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
"smallvec 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1638,7 +1636,7 @@ dependencies = [
"failure 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
"ipconfig 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"lru-cache 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"resolv-conf 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1844,7 +1842,7 @@ name = "yaml-rust"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"linked-hash-map 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"linked-hash-map 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata]
@ -1917,7 +1915,7 @@ dependencies = [
"checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a"
"checksum lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca488b89a5657b0a2ecd45b95609b3e848cf1755da332a0da46e2b2b1cb371a7"
"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1"
"checksum lazycell 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e26d4c411b39f0afcf2ba6fe502be90e6c9b299c952dbd86124782520a13cffd"
"checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d"
"checksum libflate 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "21138fc6669f438ed7ae3559d5789a5f0ba32f28c1f0608d1e452b0bb06ee936"

1
Cargo.toml

@ -20,4 +20,5 @@ serde_derive = "1.0"
serde_yaml = "0.8"
futures = "0.1"
actix-web = "0.7"
lazy_static = "1.2.0"
cloudflare = { git = "ssh://git@gitlab.sothr.com:9141/github/cloudflare-rs.git", branch = "develop" }

16
src/config.rs

@ -6,9 +6,9 @@ use std::io::prelude::*;
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct ServerConfig {
pub host: String,
pub port: i64,
pub workers: i64
pub host: Option<String>,
pub port: Option<i32>,
pub workers: Option<usize>
}
@ -50,9 +50,9 @@ pub struct Config {
fn get_default_config() -> Config {
Config {
server: ServerConfig {
host: String::from("localhost"),
port: 8080,
workers: 4
host: Option::Some(String::from("localhost")),
port: Option::Some(8080),
workers: Option::Some(4)
},
cloudflare: CloudflareConfig {
email: String::from("something@something.com"),
@ -91,6 +91,7 @@ impl fmt::Display for ConfigError {
fn read_config(yaml_str: &str) -> Result<Config, ConfigError> {
match serde_yaml::from_str(yaml_str) {
Ok(v) => Result::Ok(v),
// File wasn't valid Config/YAML
Err(_e) => Result::Err(ConfigError{})
}
}
@ -104,12 +105,15 @@ pub fn load_config(path: &str) -> Result<Config, ConfigError> {
if c > 0 {
read_config(&contents)
} else {
// File was empty
Result::Err(ConfigError{})
}
},
// File couldn't be read
Err(_e) => Result::Err(ConfigError{})
}
},
// File Doesn't exist
Err(_e) => Result::Err(ConfigError{})
}
}

51
src/main.rs

@ -2,6 +2,8 @@
extern crate serde_derive;
#[macro_use]
extern crate log;
#[macro_use]
extern crate lazy_static;
extern crate actix_web;
extern crate bytes;
@ -18,6 +20,14 @@ mod server;
mod config;
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
const DEFAULT_HOST: &'static str = "localhost";
const DEFAULT_PORT: i32 = 8080;
lazy_static! {
static ref DEFAULT_PORT_STR: String = String::from(DEFAULT_PORT.to_string());
static ref DEFAULT_WORKERS: usize = num_cpus::get();
static ref DEFAULT_WORKERS_STR: String = String::from(DEFAULT_WORKERS.to_string());
}
fn main() {
match std::env::var("RUST_LOG") {
@ -42,36 +52,53 @@ fn main() {
.short("h")
.long("host")
.value_name("HOST")
.default_value("localhost")
.default_value(&DEFAULT_HOST)
.help("The address the server listens on.")
.takes_value(true),
Arg::with_name("port")
.short("p")
.long("port")
.value_name("PORT")
.default_value("8080")
.default_value(&DEFAULT_PORT_STR)
.help("The port to run the server on.")
.takes_value(true),
Arg::with_name("workers")
.short("w")
.long("workers")
.value_name("NUMBER")
.default_value(&DEFAULT_WORKERS_STR)
.help("The number of workers to serve requests with.")
.takes_value(true),
])
.get_matches();
let config_path: &str = args.value_of("config").unwrap_or("/etc/rsddns/rsddns.yml");
let config = config::load_config(config_path).unwrap();
let host: &str = args.value_of("host").unwrap_or(&config.server.host);
let port: i32 = args
.value_of("port")
.unwrap()
.parse::<i32>()
.unwrap_or(8080);
let workers: usize = match args.value_of("workers") {
Some(count) => count.parse::<usize>().unwrap_or(num_cpus::get()),
None => num_cpus::get(),
let config = match config::load_config(config_path) {
Ok(c) => Option::Some(c),
Err(_e) => Option::None
};
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(),
}
};
info!(

Loading…
Cancel
Save