diff --git a/Cargo.lock b/Cargo.lock index ab256f3..b49b6f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1134,6 +1134,7 @@ dependencies = [ "env_logger 0.5.13 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.5 (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.78 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)", "yaml-rust 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index f660ee8..07fc88d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ bytes = "0.4.10" clap = "2.32.0" log = "0.4.0" env_logger = "0.5.13" +num_cpus = "1.0" yaml-rust = "0.4.0" serde = "1.0.78" serde_derive = "1.0.78" diff --git a/src/main.rs b/src/main.rs index b98d58a..33f8576 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,11 +8,11 @@ extern crate bytes; extern crate clap; extern crate env_logger; extern crate futures; +extern crate num_cpus; extern crate serde; use clap::{App, Arg}; -mod router; mod server; const VERSION: &'static str = env!("CARGO_PKG_VERSION"); @@ -36,6 +36,13 @@ fn main() { .default_value("/etc/rsddns/rsddns.yml") .help("Set a custom configuration file path.") .takes_value(true), + Arg::with_name("host") + .short("h") + .long("host") + .value_name("HOST") + .default_value("localhost") + .help("The address the server listens on.") + .takes_value(true), Arg::with_name("port") .short("p") .long("port") @@ -43,20 +50,35 @@ fn main() { .default_value("8080") .help("The port to run the server on.") .takes_value(true), + Arg::with_name("workers") + .short("w") + .long("workers") + .value_name("NUMBER") + .help("The number of workers to serve requests with.") + .takes_value(true), ]) .get_matches(); let config: &str = args.value_of("config").unwrap_or("/etc/rsddns/rsddns.yml"); + let host: &str = args.value_of("host").unwrap_or("localhost"); let port: i32 = args .value_of("port") .unwrap() .parse::() .unwrap_or(8080); + let workers: usize = match args.value_of("workers") { + Some(count) => count.parse::().unwrap_or(num_cpus::get()), + None => num_cpus::get(), + }; - info!("Starting server on {} with config {}", port, config); + info!( + "Starting server on {}:{} with workers={} and config {}", + host, port, workers, config + ); - actix_web::server::new(|| router::create()) - .bind(format!("localhost:{}", port)) + actix_web::server::new(|| server::router::create()) + .workers(workers) + .bind(format!("{}:{}", host, port)) .unwrap() .run(); } diff --git a/src/server/mod.rs b/src/server/mod.rs index f5f7b4f..1e621b0 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -3,6 +3,7 @@ use actix_web::{HttpRequest, Json, Result}; use VERSION; pub mod api; +pub mod router; #[derive(Serialize)] pub struct Health { diff --git a/src/router.rs b/src/server/router.rs similarity index 100% rename from src/router.rs rename to src/server/router.rs