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:
parent
e238a7b168
commit
2a54043afc
2 changed files with 7 additions and 5 deletions
|
@ -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"
|
||||
|
|
10
src/main.rs
10
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::<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...");
|
||||
|
||||
|
|
Loading…
Reference in a new issue