From 324c1f7cd6a3427bd4c84acf7936b002dbae9e68 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Sun, 29 Aug 2021 19:49:32 -0500 Subject: [PATCH] :recycle: refactor: remove pointless `Option <>` for file server root --- crates/ptth_file_server_bin/src/main.rs | 2 +- crates/ptth_server/src/bin/ptth_server.rs | 2 +- crates/ptth_server/src/file_server.rs | 9 +++------ crates/ptth_server/src/lib.rs | 6 +++--- crates/ptth_server_gui/src/main.rs | 2 +- 5 files changed, 9 insertions(+), 12 deletions(-) diff --git a/crates/ptth_file_server_bin/src/main.rs b/crates/ptth_file_server_bin/src/main.rs index 1966acb..16dbd2a 100644 --- a/crates/ptth_file_server_bin/src/main.rs +++ b/crates/ptth_file_server_bin/src/main.rs @@ -95,7 +95,7 @@ async fn main () -> anyhow::Result <()> { }); let state = Arc::new (FileServer::new ( - config_file.file_server_root, + config_file.file_server_root.unwrap_or_else (|| PathBuf::from (".")), &PathBuf::new (), config_file.name.unwrap_or_else (|| "PTTH File Server".to_string ()), metrics_interval, diff --git a/crates/ptth_server/src/bin/ptth_server.rs b/crates/ptth_server/src/bin/ptth_server.rs index 4a47bc6..ce87317 100644 --- a/crates/ptth_server/src/bin/ptth_server.rs +++ b/crates/ptth_server/src/bin/ptth_server.rs @@ -105,7 +105,7 @@ async fn main () -> Result <(), anyhow::Error> { name: opt.name.or (config_file.name).ok_or (anyhow::anyhow! ("`name` must be provided in command line or config file"))?, api_key: config_file.api_key, relay_url: opt.relay_url.or (config_file.relay_url).ok_or (anyhow::anyhow! ("`--relay-url` must be provided in command line or `relay_url` in config file"))?, - file_server_root: opt.file_server_root.or (config_file.file_server_root), + file_server_root: opt.file_server_root.or (config_file.file_server_root).unwrap_or_else (|| PathBuf::from (".")), throttle_upload: opt.throttle_upload, }; diff --git a/crates/ptth_server/src/file_server.rs b/crates/ptth_server/src/file_server.rs index 4c4d301..d09c8c2 100644 --- a/crates/ptth_server/src/file_server.rs +++ b/crates/ptth_server/src/file_server.rs @@ -56,7 +56,7 @@ use errors::FileServerError; #[derive (Default)] pub struct Config { - pub file_server_root: Option , + pub file_server_root: PathBuf, } pub struct FileServer { @@ -69,7 +69,7 @@ pub struct FileServer { impl FileServer { pub fn new ( - file_server_root: Option , + file_server_root: PathBuf, asset_root: &Path, name: String, metrics_interval: Arc >>, @@ -364,10 +364,7 @@ impl FileServer { resp } - let default_root = PathBuf::from ("./"); - let root: &std::path::Path = self.config.file_server_root - .as_ref () - .unwrap_or (&default_root); + let root: &std::path::Path = &self.config.file_server_root; Ok (match internal::serve_all (root, method, uri, headers, self.hidden_path.as_deref ()).await? { Favicon => serve_error (StatusCode::NotFound, "Not found\n"), diff --git a/crates/ptth_server/src/lib.rs b/crates/ptth_server/src/lib.rs index 4f4875f..5f5ac90 100644 --- a/crates/ptth_server/src/lib.rs +++ b/crates/ptth_server/src/lib.rs @@ -214,7 +214,7 @@ pub struct ConfigFile { /// Directory that the file server module will expose to clients /// over the relay. If None, the current working dir is used. - pub file_server_root: Option , + pub file_server_root: PathBuf, /// For debugging. pub throttle_upload: bool, @@ -227,7 +227,7 @@ impl ConfigFile { name, api_key, relay_url, - file_server_root: None, + file_server_root: PathBuf::from ("."), throttle_upload: false, } } @@ -267,7 +267,7 @@ impl Builder { name, api_key: ptth_core::gen_key (), relay_url, - file_server_root: None, + file_server_root: PathBuf::from ("."), throttle_upload: false, }; diff --git a/crates/ptth_server_gui/src/main.rs b/crates/ptth_server_gui/src/main.rs index 18db709..c7f4ca1 100644 --- a/crates/ptth_server_gui/src/main.rs +++ b/crates/ptth_server_gui/src/main.rs @@ -57,7 +57,7 @@ fn main () name: gui.input_name.value ().to_string (), api_key: gui.input_api_key.value ().to_string (), relay_url: gui.input_relay_url.value ().to_string (), - file_server_root: Some (std::path::PathBuf::from (gui.input_file_server_root.value ())), + file_server_root: std::path::PathBuf::from (gui.input_file_server_root.value ()), throttle_upload: false, };