From 2103ea0ed4df223b238dda96f142814692ed861d Mon Sep 17 00:00:00 2001 From: jikstra Date: Wed, 29 Dec 2021 04:10:24 +0100 Subject: Implement --route-prefix to set specific route prefix --- src/args.rs | 6 +++++- src/config.rs | 15 ++++++++------- src/listing.rs | 8 +++----- src/main.rs | 11 +++++++---- src/renderer.rs | 7 ++----- 5 files changed, 25 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/args.rs b/src/args.rs index 88ad8fc..b733769 100644 --- a/src/args.rs +++ b/src/args.rs @@ -60,8 +60,12 @@ pub struct CliArgs { )] pub auth: Vec, + /// Use a specific route prefix + #[clap(long = "route-prefix")] + pub route_prefix: Option, + /// Generate a random 6-hexdigit route - #[clap(long = "random-route")] + #[clap(long = "random-route", conflicts_with("route-prefix"))] pub random_route: bool, /// Do not follow symbolic links diff --git a/src/config.rs b/src/config.rs index fda2f84..4b9d2dc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -47,8 +47,8 @@ pub struct MiniserveConfig { /// Show hidden files pub show_hidden: bool, - /// Enable random route generation - pub random_route: Option, + /// Route prefix + pub route_prefix: String, /// Randomly generated favicon route pub favicon_route: String, @@ -131,10 +131,11 @@ impl MiniserveConfig { ] }; - let random_route = if args.random_route { - Some(nanoid::nanoid!(6, &ROUTE_ALPHABET)) - } else { - None + let route_prefix = match (args.route_prefix, args.random_route) { + (Some(prefix), _) if prefix.starts_with('/') => prefix, + (Some(prefix), _) => format!("/{}", prefix), + (_, true) => format!("/{}", nanoid::nanoid!(6, &ROUTE_ALPHABET)), + _ => "".to_owned(), }; // Generate some random routes for the favicon and css so that they are very unlikely to conflict with @@ -185,7 +186,7 @@ impl MiniserveConfig { path_explicitly_chosen, no_symlinks: args.no_symlinks, show_hidden: args.hidden, - random_route, + route_prefix, favicon_route, css_route, default_color_scheme, diff --git a/src/listing.rs b/src/listing.rs index 9273025..4360c87 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -162,7 +162,7 @@ pub fn directory_listing( let serve_path = req.path(); let base = Path::new(serve_path); - let random_route_abs = format!("/{}", conf.random_route.clone().unwrap_or_default()); + let random_route_abs = format!("/{}", conf.route_prefix); let is_root = base.parent().is_none() || Path::new(&req.path()) == Path::new(&random_route_abs); let encoded_dir = match base.strip_prefix(random_route_abs) { @@ -181,10 +181,8 @@ pub fn directory_listing( let decoded = percent_decode_str(&encoded_dir).decode_utf8_lossy(); let mut res: Vec = Vec::new(); - let mut link_accumulator = match &conf.random_route { - Some(random_route) => format!("/{}/", random_route), - None => "/".to_string(), - }; + + let mut link_accumulator = format!("{}/", &conf.route_prefix); let mut components = Path::new(&*decoded).components().peekable(); diff --git a/src/main.rs b/src/main.rs index d5cac00..3cbed1d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -165,9 +165,12 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> { Some(_) => format!("https://{}", addr), None => format!("http://{}", addr), }) - .map(|url| match miniserve_config.random_route { - Some(ref random_route) => format!("{}/{}", url, random_route), - None => url, + .map(|url| { + if !miniserve_config.route_prefix.is_empty() { + format!("{}{}", url, miniserve_config.route_prefix) + } else { + url + } }) .collect::>() }; @@ -195,7 +198,7 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> { ) .route(&format!("/{}", inside_config.css_route), web::get().to(css)) .service( - web::scope(inside_config.random_route.as_deref().unwrap_or("")) + web::scope(&inside_config.route_prefix) .wrap(middleware::Condition::new( !inside_config.auth.is_empty(), HttpAuthentication::basic(auth::handle_auth), diff --git a/src/renderer.rs b/src/renderer.rs index c2c4cb5..0ac5077 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -25,10 +25,7 @@ pub fn page( return raw(entries, is_root); } - let upload_route = match conf.random_route { - Some(ref random_route) => format!("/{}/upload", random_route), - None => "/upload".to_string(), - }; + let upload_route = format!("{}/upload", &conf.route_prefix); let (sort_method, sort_order) = (query_params.sort, query_params.order); let upload_action = build_upload_action(&upload_route, encoded_dir, sort_method, sort_order); @@ -578,7 +575,7 @@ pub fn render_error( p { (error) } } // WARN don't expose random route! - @if conf.random_route.is_none() { + @if conf.route_prefix.is_empty() { div.error-nav { a.error-back href=(return_address) { "Go back to file listing" -- cgit v1.2.3 From d64b2544e53a474f93e4ad3d3ff87639930b906b Mon Sep 17 00:00:00 2001 From: Jikstra <34889164+Jikstra@users.noreply.github.com> Date: Mon, 3 Jan 2022 20:14:04 +0100 Subject: Update src/config.rs Co-authored-by: Ali MJ Al-Nasrawy --- src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/config.rs b/src/config.rs index 4b9d2dc..94e232f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -133,7 +133,7 @@ impl MiniserveConfig { let route_prefix = match (args.route_prefix, args.random_route) { (Some(prefix), _) if prefix.starts_with('/') => prefix, - (Some(prefix), _) => format!("/{}", prefix), + (Some(prefix), _) => format!("/{}", prefix.trim_matches('/')), (_, true) => format!("/{}", nanoid::nanoid!(6, &ROUTE_ALPHABET)), _ => "".to_owned(), }; -- cgit v1.2.3 From 020516c92b220ca47343e360bbec0686899730c0 Mon Sep 17 00:00:00 2001 From: jikstra Date: Mon, 3 Jan 2022 20:28:03 +0100 Subject: Apply requested changes --- src/main.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 3cbed1d..4e16f83 100644 --- a/src/main.rs +++ b/src/main.rs @@ -165,13 +165,7 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> { Some(_) => format!("https://{}", addr), None => format!("http://{}", addr), }) - .map(|url| { - if !miniserve_config.route_prefix.is_empty() { - format!("{}{}", url, miniserve_config.route_prefix) - } else { - url - } - }) + .map(|url| format!("{}{}", url, miniserve_config.route_prefix)) .collect::>() }; -- cgit v1.2.3 From 48f192e3499ee9609660a3e8bfcba22cc4a93ee8 Mon Sep 17 00:00:00 2001 From: Jikstra <34889164+Jikstra@users.noreply.github.com> Date: Tue, 4 Jan 2022 18:08:03 +0100 Subject: Apply alimjays suggestion Co-authored-by: Ali MJ Al-Nasrawy --- src/config.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/config.rs b/src/config.rs index 94e232f..b163e96 100644 --- a/src/config.rs +++ b/src/config.rs @@ -132,7 +132,6 @@ impl MiniserveConfig { }; let route_prefix = match (args.route_prefix, args.random_route) { - (Some(prefix), _) if prefix.starts_with('/') => prefix, (Some(prefix), _) => format!("/{}", prefix.trim_matches('/')), (_, true) => format!("/{}", nanoid::nanoid!(6, &ROUTE_ALPHABET)), _ => "".to_owned(), -- cgit v1.2.3