Skip to content

Commit

Permalink
Simplify listener definition (#681)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
spenserblack committed Jan 12, 2023
1 parent e238a7b commit 2a54043
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -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"
Expand Down
10 changes: 6 additions & 4 deletions src/main.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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(
Expand All @@ -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::<String>("address").unwrap();
let port = matches.get_one::<String>("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...");

Expand Down

0 comments on commit 2a54043

Please sign in to comment.