aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorcyqsimon <28627918+cyqsimon@users.noreply.github.com>2022-07-20 08:59:07 +0000
committercyqsimon <28627918+cyqsimon@users.noreply.github.com>2022-07-20 09:42:57 +0000
commitf9e1806f203512ede8b9ea9324fcf537ef70296c (patch)
treedd156cd71918650a4339bb9248bb1101b79875d6 /src/main.rs
parentrefactor `configure_app` (diff)
downloadminiserve-f9e1806f203512ede8b9ea9324fcf537ef70296c.tar.gz
miniserve-f9e1806f203512ede8b9ea9324fcf537ef70296c.zip
Minor refactor on `run`
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs52
1 files changed, 16 insertions, 36 deletions
diff --git a/src/main.rs b/src/main.rs
index 0beb285..5e6f2c8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,6 @@
use std::io;
use std::io::Write;
use std::net::{IpAddr, SocketAddr, TcpListener};
-use std::path::{Path, PathBuf};
use std::thread;
use std::time::Duration;
@@ -69,33 +68,19 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> {
simplelog::LevelFilter::Warn
};
- if simplelog::TermLogger::init(
+ simplelog::TermLogger::init(
log_level,
simplelog::Config::default(),
simplelog::TerminalMode::Mixed,
simplelog::ColorChoice::Auto,
)
- .is_err()
- {
- simplelog::SimpleLogger::init(log_level, simplelog::Config::default())
- .expect("Couldn't initialize logger")
- }
+ .or_else(|_| simplelog::SimpleLogger::init(log_level, simplelog::Config::default()))
+ .expect("Couldn't initialize logger");
- if miniserve_config.no_symlinks {
- let is_symlink = miniserve_config
- .path
- .symlink_metadata()
- .map_err(|e| {
- ContextualError::IoError("Failed to retrieve symlink's metadata".to_string(), e)
- })?
- .file_type()
- .is_symlink();
-
- if is_symlink {
- return Err(ContextualError::NoSymlinksOptionWithSymlinkServePath(
- miniserve_config.path.to_string_lossy().to_string(),
- ));
- }
+ if miniserve_config.no_symlinks && miniserve_config.path.is_symlink() {
+ return Err(ContextualError::NoSymlinksOptionWithSymlinkServePath(
+ miniserve_config.path.to_string_lossy().to_string(),
+ ));
}
let inside_config = miniserve_config.clone();
@@ -104,7 +89,15 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> {
ContextualError::IoError("Failed to resolve path to be served".to_string(), e)
})?;
- check_file_exists(&canon_path, &miniserve_config.index);
+ // warn if --index is specified but not found
+ if let Some(ref index) = miniserve_config.index {
+ if !canon_path.join(index).exists() {
+ warn!(
+ "The file '{}' provided for option --index could not be found.",
+ index.to_string_lossy(),
+ );
+ }
+ }
let path_string = canon_path.to_string_lossy();
@@ -266,19 +259,6 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> {
.map_err(|e| ContextualError::IoError("".to_owned(), e))
}
-fn check_file_exists(canon_path: &Path, file_option: &Option<PathBuf>) {
- if let Some(file_path) = file_option {
- let file_path: &Path = file_path.as_ref();
- let has_file: std::path::PathBuf = [canon_path, file_path].iter().collect();
- if !has_file.exists() {
- error!(
- "The file '{}' provided for option --index could not be found.",
- file_path.to_string_lossy(),
- );
- }
- }
-}
-
/// Allows us to set low-level socket options
///
/// This mainly used to set `set_only_v6` socket option