From c10bc016ef651b390c3cccc53bad6c45ec6cf765 Mon Sep 17 00:00:00 2001 From: Eli Flanagan Date: Fri, 6 Sep 2019 12:49:08 -0400 Subject: add a default index serving option --- src/args.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/args.rs b/src/args.rs index 47f4d1f..b4b50e2 100644 --- a/src/args.rs +++ b/src/args.rs @@ -27,6 +27,10 @@ struct CLIArgs { #[structopt(name = "PATH", parse(from_os_str))] path: Option, + /// serve index.* files by default + #[structopt(short = "d", long)] + default_index: bool, + /// Port to use #[structopt(short = "p", long = "port", default_value = "8080")] port: u16, -- cgit v1.2.3 From d68a874d18e79eecb7f6a4ba941a3494e408322a Mon Sep 17 00:00:00 2001 From: Eli Flanagan Date: Sat, 7 Sep 2019 20:50:15 -0400 Subject: wire up arguments, index.html serving still broken --- src/args.rs | 1 + src/main.rs | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/args.rs b/src/args.rs index b4b50e2..6a3a4a5 100644 --- a/src/args.rs +++ b/src/args.rs @@ -166,6 +166,7 @@ pub fn parse_args() -> crate::MiniserveConfig { no_symlinks: args.no_symlinks, random_route, default_color_scheme, + default_index: args.default_index, overwrite_files: args.overwrite_files, file_upload: args.file_upload, } diff --git a/src/main.rs b/src/main.rs index dc98df0..46c38d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ use simplelog::{Config, LevelFilter, TermLogger, TerminalMode}; use std::io::{self, Write}; use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::thread; +use std::path::PathBuf; use std::time::Duration; use yansi::{Color, Paint}; @@ -52,6 +53,9 @@ pub struct MiniserveConfig { /// Default color scheme pub default_color_scheme: themes::ColorScheme, + /// Serve index.* files by default + pub default_index: bool, + /// Enable file upload pub file_upload: bool, @@ -234,6 +238,13 @@ fn configure_app(app: App) -> App { }; if path.is_file() { None + } else if app.state().default_index == true { + let mut index_path = PathBuf::from(path); + index_path.push("index.html"); + Some( + fs::StaticFiles::new(index_path) + .expect("Failed to setup static file handler") + ) } else { let u_r = upload_route.clone(); Some( -- cgit v1.2.3 From a861bd22855f83163fa00f59de04b11628c9e7a5 Mon Sep 17 00:00:00 2001 From: Eli Flanagan Date: Mon, 9 Sep 2019 12:20:42 -0400 Subject: use actix-web method for static index --- src/main.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 46c38d1..f2dd351 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,6 @@ use simplelog::{Config, LevelFilter, TermLogger, TerminalMode}; use std::io::{self, Write}; use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::thread; -use std::path::PathBuf; use std::time::Duration; use yansi::{Color, Paint}; @@ -239,11 +238,10 @@ fn configure_app(app: App) -> App { if path.is_file() { None } else if app.state().default_index == true { - let mut index_path = PathBuf::from(path); - index_path.push("index.html"); Some( - fs::StaticFiles::new(index_path) + fs::StaticFiles::new(path) .expect("Failed to setup static file handler") + .index_file("index.html") ) } else { let u_r = upload_route.clone(); -- cgit v1.2.3 From b3f170e7edf77e6a37f11af8701d427a9b2fe204 Mon Sep 17 00:00:00 2001 From: Eli Flanagan Date: Thu, 12 Sep 2019 19:57:21 -0400 Subject: change to path option for configurability --- src/args.rs | 8 ++++---- src/main.rs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/args.rs b/src/args.rs index 6a3a4a5..d31cb26 100644 --- a/src/args.rs +++ b/src/args.rs @@ -27,9 +27,9 @@ struct CLIArgs { #[structopt(name = "PATH", parse(from_os_str))] path: Option, - /// serve index.* files by default - #[structopt(short = "d", long)] - default_index: bool, + /// name of an index files to serve by default + #[structopt(long, parse(from_os_str), name="index_file")] + index: Option, /// Port to use #[structopt(short = "p", long = "port", default_value = "8080")] @@ -166,7 +166,7 @@ pub fn parse_args() -> crate::MiniserveConfig { no_symlinks: args.no_symlinks, random_route, default_color_scheme, - default_index: args.default_index, + index: args.index, overwrite_files: args.overwrite_files, file_upload: args.file_upload, } diff --git a/src/main.rs b/src/main.rs index f2dd351..5db9b31 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,8 +52,8 @@ pub struct MiniserveConfig { /// Default color scheme pub default_color_scheme: themes::ColorScheme, - /// Serve index.* files by default - pub default_index: bool, + /// name of an index files to serve by default + pub index: Option, /// Enable file upload pub file_upload: bool, @@ -237,11 +237,11 @@ fn configure_app(app: App) -> App { }; if path.is_file() { None - } else if app.state().default_index == true { + } else if let Some(index_file) = &app.state().index { Some( fs::StaticFiles::new(path) .expect("Failed to setup static file handler") - .index_file("index.html") + .index_file(index_file.to_string_lossy()) ) } else { let u_r = upload_route.clone(); -- cgit v1.2.3 From cd03f984bacbcacc57d5ce07c13bc401b48c09f9 Mon Sep 17 00:00:00 2001 From: Eli Flanagan Date: Thu, 12 Sep 2019 20:14:07 -0400 Subject: warn user if provided index file is not found --- src/main.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main.rs b/src/main.rs index 5db9b31..d5055b9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -122,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!( -- cgit v1.2.3 From f92fb9bd401198dbab27edf3bbd2435a104a1c72 Mon Sep 17 00:00:00 2001 From: Eli Flanagan Date: Sat, 14 Sep 2019 12:45:22 -0400 Subject: fix typo --- shell.nix | 17 +++++++++++++++++ src/args.rs | 2 +- src/main.rs | 2 +- 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 shell.nix diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..8da2c58 --- /dev/null +++ b/shell.nix @@ -0,0 +1,17 @@ +# copied expressions from https://nixos.wiki/wiki/Rust +# and Mozilla's nix overlay README +let + moz_overlay = import (builtins.fetchTarball https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz); + nixpkgs = import { overlays = [ moz_overlay ]; }; +in + with nixpkgs; + stdenv.mkDerivation { + name = "rust_nightly_shell"; + buildInputs = [ + nixpkgs.latest.rustChannels.nightly.rust + openssl + # needed to correctly populate the + # nix specific paths to openssl libraries + pkgconfig + ]; + } diff --git a/src/args.rs b/src/args.rs index d31cb26..0d4e3ce 100644 --- a/src/args.rs +++ b/src/args.rs @@ -27,7 +27,7 @@ struct CLIArgs { #[structopt(name = "PATH", parse(from_os_str))] path: Option, - /// name of an index files to serve by default + /// name of an index file to serve by default #[structopt(long, parse(from_os_str), name="index_file")] index: Option, diff --git a/src/main.rs b/src/main.rs index d5055b9..3bb521c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,7 +52,7 @@ pub struct MiniserveConfig { /// Default color scheme pub default_color_scheme: themes::ColorScheme, - /// name of an index files to serve by default + /// name of an index file to serve by default pub index: Option, /// Enable file upload -- cgit v1.2.3 From 0a3fd7ca83dfa9b53565de5c46634864504e52e6 Mon Sep 17 00:00:00 2001 From: Eli Flanagan Date: Thu, 19 Sep 2019 19:10:16 -0400 Subject: remove shell.nix --- shell.nix | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 shell.nix diff --git a/shell.nix b/shell.nix deleted file mode 100644 index 8da2c58..0000000 --- a/shell.nix +++ /dev/null @@ -1,17 +0,0 @@ -# copied expressions from https://nixos.wiki/wiki/Rust -# and Mozilla's nix overlay README -let - moz_overlay = import (builtins.fetchTarball https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz); - nixpkgs = import { overlays = [ moz_overlay ]; }; -in - with nixpkgs; - stdenv.mkDerivation { - name = "rust_nightly_shell"; - buildInputs = [ - nixpkgs.latest.rustChannels.nightly.rust - openssl - # needed to correctly populate the - # nix specific paths to openssl libraries - pkgconfig - ]; - } -- cgit v1.2.3 From 03df0b916ab6f88e8523fcce549b835369972082 Mon Sep 17 00:00:00 2001 From: Eli Flanagan Date: Tue, 24 Sep 2019 20:20:49 -0400 Subject: add a test for missing index file --- tests/serve_request.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/serve_request.rs b/tests/serve_request.rs index 7419ab1..f48065f 100644 --- a/tests/serve_request.rs +++ b/tests/serve_request.rs @@ -6,6 +6,7 @@ use fixtures::{port, tmpdir, Error, DIRECTORIES, FILES}; use rstest::rstest; use select::document::Document; use select::node::Node; +use std::io::BufReader; use std::process::{Command, Stdio}; use std::thread::sleep; use std::time::Duration; @@ -55,7 +56,10 @@ fn serves_requests_with_non_default_port(tmpdir: TempDir, port: u16) -> Result<( .error_for_status()?; let dir_body_parsed = Document::from_read(dir_body)?; for &file in FILES { - assert!(dir_body_parsed.find(|x: &Node| x.text() == file).next().is_some()); + assert!(dir_body_parsed + .find(|x: &Node| x.text() == file) + .next() + .is_some()); } } @@ -63,3 +67,24 @@ fn serves_requests_with_non_default_port(tmpdir: TempDir, port: u16) -> Result<( Ok(()) } + +#[rstest] +fn serves_requests_custom_index_notice(tmpdir: TempDir) -> Result<(), Error> { + let mut child = Command::cargo_bin("miniserve")? + .arg("--index=not.html") + .arg(tmpdir.path()) + .stdout(Stdio::piped()) + .spawn()?; + + sleep(Duration::from_secs(1)); + + child.kill()?; + let output = child.wait_with_output().expect("Failed to read stdout"); + let all_text = String::from_utf8(output.stdout); + + assert!(all_text + .unwrap() + .contains("The provided index file could not be found")); + + Ok(()) +} -- cgit v1.2.3 From bbe3cb3975f011b40d94c155bc0e80e0bd6ce391 Mon Sep 17 00:00:00 2001 From: Eli Flanagan Date: Mon, 3 Feb 2020 15:29:53 -0500 Subject: add Lucretiel's documentation suggestion --- src/args.rs | 5 ++++- src/main.rs | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/args.rs b/src/args.rs index ad5f1d6..95ddaf7 100644 --- a/src/args.rs +++ b/src/args.rs @@ -27,7 +27,10 @@ struct CLIArgs { #[structopt(name = "PATH", parse(from_os_str))] path: Option, - /// name of an index file to serve by default + /// The name of a directory index file to serve, like "index.html" + /// + /// Normally, when miniserve serves a directory, it creates a listing for that directory. + /// However, if a directory contains this file, miniserve will serve that file instead. #[structopt(long, parse(from_os_str), name="index_file")] index: Option, diff --git a/src/main.rs b/src/main.rs index c31341e..11cf5c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,7 +52,10 @@ pub struct MiniserveConfig { /// Default color scheme pub default_color_scheme: themes::ColorScheme, - /// name of an index file to serve by default + /// The name of a directory index file to serve, like "index.html" + /// + /// Normally, when miniserve serves a directory, it creates a listing for that directory. + /// However, if a directory contains this file, miniserve will serve that file instead. pub index: Option, /// Enable file upload -- cgit v1.2.3 From dc9b3663765bedb0922cd26a10cbeb5864e484c8 Mon Sep 17 00:00:00 2001 From: Eli Flanagan Date: Mon, 3 Feb 2020 16:00:28 -0500 Subject: specify port to avoid test conflict --- tests/serve_request.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/serve_request.rs b/tests/serve_request.rs index 9f6b727..064e196 100644 --- a/tests/serve_request.rs +++ b/tests/serve_request.rs @@ -68,9 +68,11 @@ fn serves_requests_with_non_default_port(tmpdir: TempDir, port: u16) -> Result<( } #[rstest] -fn serves_requests_custom_index_notice(tmpdir: TempDir) -> Result<(), Error> { +fn serves_requests_custom_index_notice(tmpdir: TempDir, port: u16) -> Result<(), Error> { let mut child = Command::cargo_bin("miniserve")? .arg("--index=not.html") + .arg("-p") + .arg(port.to_string()) .arg(tmpdir.path()) .stdout(Stdio::piped()) .spawn()?; -- cgit v1.2.3