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.
This commit is contained in:
Spenser Black 2023-01-12 03:41:59 -05:00 committed by GitHub
parent e238a7b168
commit 2a54043afc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 5 deletions

View File

@ -10,7 +10,7 @@ edition = "2021"
[dependencies] [dependencies]
askama = { version = "0.11.1", default-features = false } askama = { version = "0.11.1", default-features = false }
cached = "0.40.0" 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" regex = "1.7.0"
serde = { version = "1.0.147", features = ["derive"] } serde = { version = "1.0.147", features = ["derive"] }
cookie = "0.16.1" cookie = "0.16.1"

View File

@ -13,7 +13,7 @@ mod user;
mod utils; mod utils;
// Import Crates // Import Crates
use clap::{Arg, Command}; use clap::{Arg, ArgAction, Command};
use futures_lite::FutureExt; use futures_lite::FutureExt;
use hyper::{header::HeaderValue, Body, Request, Response}; use hyper::{header::HeaderValue, Body, Request, Response};
@ -130,8 +130,10 @@ async fn main() {
.short('p') .short('p')
.long("port") .long("port")
.value_name("PORT") .value_name("PORT")
.env("PORT")
.help("Port to listen on") .help("Port to listen on")
.default_value("8080") .default_value("8080")
.action(ArgAction::Set)
.num_args(1), .num_args(1),
) )
.arg( .arg(
@ -145,11 +147,11 @@ async fn main() {
) )
.get_matches(); .get_matches();
let address = matches.get_one("address").map(|m: &String| m.as_str()).unwrap_or("0.0.0.0"); let address = matches.get_one::<String>("address").unwrap();
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 port = matches.get_one::<String>("port").unwrap();
let hsts = matches.get_one("hsts").map(|m: &String| m.as_str()); 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..."); println!("Starting Libreddit...");