diff options
author | Sven-Hendrik Haase <svenstaro@gmail.com> | 2021-10-25 23:56:22 +0000 |
---|---|---|
committer | Sven-Hendrik Haase <svenstaro@gmail.com> | 2021-10-25 23:56:22 +0000 |
commit | 443c19783693e8900c286d3f1841ee57001e5937 (patch) | |
tree | b51da9d1c7c8857e87c06c305d2f680fa6c77af1 | |
parent | Fix lint (diff) | |
download | miniserve-443c19783693e8900c286d3f1841ee57001e5937.tar.gz miniserve-443c19783693e8900c286d3f1841ee57001e5937.zip |
Refactor SPA-mode to make it more ergonomic
-rw-r--r-- | src/args.rs | 12 | ||||
-rw-r--r-- | src/config.rs | 20 | ||||
-rw-r--r-- | src/main.rs | 36 |
3 files changed, 35 insertions, 33 deletions
diff --git a/src/args.rs b/src/args.rs index 6f7b1b1..a5a61d1 100644 --- a/src/args.rs +++ b/src/args.rs @@ -26,13 +26,13 @@ pub struct CliArgs { #[clap(long, parse(from_os_str), name = "index_file", value_hint = ValueHint::FilePath)] pub index: Option<PathBuf>, - /// The index file of a single page application + /// Activate SPA (Single Page Application) mode /// - /// If this option is set, miniserve will serve the specified file instead of a 404 page when - /// a non-existent path is requested. This is intended for single-page applications where - /// routing takes place on the client side. - #[clap(long, parse(from_os_str), name = "spa_index_file", value_hint = ValueHint::FilePath)] - pub spa_index: Option<PathBuf>, + /// This will cause the file given by --index to be served for all non-existing file paths. In + /// effect, this will serve the index file whenever a 404 would otherwise occur in order to + /// allow the SPA router to handle the request instead. + #[clap(long, requires = "index_file")] + pub spa: bool, /// Port to use #[clap(short = 'p', long = "port", default_value = "8080")] diff --git a/src/config.rs b/src/config.rs index e0d5ec5..fda2f84 100644 --- a/src/config.rs +++ b/src/config.rs @@ -68,12 +68,12 @@ pub struct MiniserveConfig { /// However, if a directory contains this file, miniserve will serve that file instead. pub index: Option<std::path::PathBuf>, - /// The index file of a single page application + /// Activate SPA (Single Page Application) mode /// - /// If this option is set, miniserve will serve the specified file instead of a 404 page when - /// a non-existent path is requested. This is intended for single-page applications where - /// routing takes place on the client side. - pub spa_index: Option<std::path::PathBuf>, + /// This will cause the file given by `index` to be served for all non-existing file paths. In + /// effect, this will serve the index file whenever a 404 would otherwise occur in order to + /// allow the SPA router to handle the request instead. + pub spa: bool, /// Enable QR code display pub show_qrcode: bool, @@ -176,12 +176,6 @@ impl MiniserveConfig { #[cfg(not(feature = "tls"))] let tls_rustls_server_config = None; - // If spa_index is set but index is unset, copy the former into the latter - let index = match args.index { - Some(index) => Some(index), - None => args.spa_index.clone(), - }; - Ok(MiniserveConfig { verbose: args.verbose, path: args.path.unwrap_or_else(|| PathBuf::from(".")), @@ -196,8 +190,8 @@ impl MiniserveConfig { css_route, default_color_scheme, default_color_scheme_dark, - index, - spa_index: args.spa_index, + index: args.index, + spa: args.spa, overwrite_files: args.overwrite_files, show_qrcode: args.qrcode, file_upload: args.file_upload, diff --git a/src/main.rs b/src/main.rs index 205fac5..f6e78e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -97,8 +97,7 @@ 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, "index"); - check_file_exists(&canon_path, &miniserve_config.spa_index, "spa-index"); + check_file_exists(&canon_path, &miniserve_config.index); let path_string = canon_path.to_string_lossy(); @@ -264,15 +263,14 @@ 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>, option_name: &str) { +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 --{} could not be found.", + "The file '{}' provided for option --index could not be found.", file_path.to_string_lossy(), - option_name, ); } } @@ -307,15 +305,25 @@ fn configure_app(app: &mut web::ServiceConfig, conf: &MiniserveConfig) { let files_service = || { let files = actix_files::Files::new("", &conf.path); let files = match &conf.index { - Some(index_file) => files.index_file(index_file.to_string_lossy()), - None => files, - }; - let files = match &conf.spa_index { - Some(spa_index_file) => files.default_handler( - NamedFile::open(&conf.path.join(spa_index_file)) - .expect("Cant open SPA index file."), - ), - None => files.default_handler(web::to(error_404)), + Some(index_file) => { + if conf.spa { + files + .index_file(index_file.to_string_lossy()) + .default_handler( + NamedFile::open(&conf.path.join(index_file)) + .expect("Cant open SPA index file."), + ) + } else { + files.index_file(index_file.to_string_lossy()) + } + } + None => { + if conf.spa { + unreachable!("This can't be reached since we require --index to be provided if --spa is given via clap"); + } else { + files + } + } }; let files = match conf.show_hidden { true => files.use_hidden_files(), |