diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/args.rs | 5 | ||||
-rw-r--r-- | src/main.rs | 17 |
2 files changed, 22 insertions, 0 deletions
diff --git a/src/args.rs b/src/args.rs index 8fde78a..ad5f1d6 100644 --- a/src/args.rs +++ b/src/args.rs @@ -27,6 +27,10 @@ struct CLIArgs { #[structopt(name = "PATH", parse(from_os_str))] path: Option<PathBuf>, + /// name of an index file to serve by default + #[structopt(long, parse(from_os_str), name="index_file")] + index: Option<PathBuf>, + /// Port to use #[structopt(short = "p", long = "port", default_value = "8080")] port: u16, @@ -162,6 +166,7 @@ pub fn parse_args() -> crate::MiniserveConfig { no_symlinks: args.no_symlinks, random_route, default_color_scheme, + index: args.index, overwrite_files: args.overwrite_files, file_upload: args.file_upload, } diff --git a/src/main.rs b/src/main.rs index a1217d0..c31341e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,6 +52,9 @@ pub struct MiniserveConfig { /// Default color scheme pub default_color_scheme: themes::ColorScheme, + /// name of an index file to serve by default + pub index: Option<std::path::PathBuf>, + /// Enable file upload pub file_upload: bool, @@ -119,6 +122,14 @@ fn run() -> Result<(), ContextualError> { let canon_path = miniserve_config.path.canonicalize().map_err(|e| { ContextualError::IOError("Failed to resolve path to be served".to_string(), e) })?; + + if let Some(index_path) = &miniserve_config.index { + let has_index: std::path::PathBuf = [&canon_path, index_path].iter().collect(); + if !has_index.exists() { + + println!("{warning} The provided index file could not be found.", warning=Color::RGB(255, 192, 0).paint("Notice:").bold()); + } + } let path_string = canon_path.to_string_lossy(); println!( @@ -234,6 +245,12 @@ fn configure_app(app: App<MiniserveConfig>) -> App<MiniserveConfig> { }; if path.is_file() { None + } else if let Some(index_file) = &app.state().index { + Some( + fs::StaticFiles::new(path) + .expect("Failed to setup static file handler") + .index_file(index_file.to_string_lossy()) + ) } else { let u_r = upload_route.clone(); Some( |