diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-01-02 15:43:03 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-01-02 16:13:39 +0100 |
commit | ff4cd2ff87c0f2d7d9e37ece753515a95c82285c (patch) | |
tree | 0c8b92388301b0d4e175f2a558c24f9d32f6eb20 | |
parent | e6f132173b3b8c4589d2f243808400ab9439cb9a (diff) |
Make host/port configurable via CLI
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | src/main.rs | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index a2e5b10..df40cd3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,7 @@ use std::collections::HashMap; use std::fs::File; use std::io::{BufReader, BufRead}; use std::process::exit; +use std::str::FromStr; use clap::{App, Arg}; use rocket::State; @@ -222,6 +223,20 @@ fn main() { .help("configuration file to use") .takes_value(true) .required(true)) + .arg(Arg::with_name("address") + .long("address") + .value_name("ADDR") + .help("Address to bind to") + .takes_value(true) + .default_value("127.0.0.1") + .required(false)) + .arg(Arg::with_name("port") + .long("port") + .value_name("PORT") + .help("Port to bind to") + .takes_value(true) + .default_value("8000") + .required(false)) .arg(Arg::with_name("verbosity") .short("v") .long("verbose") @@ -247,8 +262,22 @@ fn main() { }, }; - rocket::ignite() - .manage(config) - .mount("/", routes![index, search, query]) - .launch(); + let host = matches.value_of("address").unwrap(); // safe by clap + let port = matches.value_of("port").map(u16::from_str) + .transpose() + .unwrap_or_else(|e| { + eprintln!("Parsing port failed: {:?}", e); + std::process::exit(1) + }) + .unwrap(); // safe by clap + + rocket::custom({ + let mut c = rocket::config::Config::production(); + c.set_address(host).expect(&format!("Using host address failed: {}", host)); + c.set_port(port); + c + }) + .manage(config) + .mount("/", routes![index, search, query]) + .launch(); } |