From 2a54043afc97acfd8f6c25af4a1eae0974708c56 Mon Sep 17 00:00:00 2001 From: Spenser Black Date: Thu, 12 Jan 2023 03:41:59 -0500 Subject: [PATCH] Simplify listener definition (#681) This simplifies the logic to build the listener by using more clap features instead of manually accessing the PORT environment variable. This also removes unnecessary `unwrap_or` calls that set defaults that are already set by clap. --- Cargo.toml | 2 +- src/main.rs | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1c8b7ae..260f4a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" [dependencies] askama = { version = "0.11.1", default-features = false } cached = "0.40.0" -clap = { version = "4.0.24", default-features = false, features = ["std"] } +clap = { version = "4.0.24", default-features = false, features = ["std", "env"] } regex = "1.7.0" serde = { version = "1.0.147", features = ["derive"] } cookie = "0.16.1" diff --git a/src/main.rs b/src/main.rs index bffe99c..2d848ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ mod user; mod utils; // Import Crates -use clap::{Arg, Command}; +use clap::{Arg, ArgAction, Command}; use futures_lite::FutureExt; use hyper::{header::HeaderValue, Body, Request, Response}; @@ -130,8 +130,10 @@ async fn main() { .short('p') .long("port") .value_name("PORT") + .env("PORT") .help("Port to listen on") .default_value("8080") + .action(ArgAction::Set) .num_args(1), ) .arg( @@ -145,11 +147,11 @@ async fn main() { ) .get_matches(); - let address = matches.get_one("address").map(|m: &String| m.as_str()).unwrap_or("0.0.0.0"); - let port = std::env::var("PORT").unwrap_or_else(|_| matches.get_one("port").map(|m: &String| m.as_str()).unwrap_or("8080").to_string()); + let address = matches.get_one::("address").unwrap(); + let port = matches.get_one::("port").unwrap(); let hsts = matches.get_one("hsts").map(|m: &String| m.as_str()); - let listener = [address, ":", &port].concat(); + let listener = [address, ":", port].concat(); println!("Starting Libreddit...");