Skip to content

Commit

Permalink
Allow custom route prefix
Browse files Browse the repository at this point in the history
Signed-off-by: Ali MJ Al-Nasrawy <alimjalnasrawy@gmail.com>
  • Loading branch information
aliemjay committed May 28, 2021
1 parent 35d471a commit 7ea3beb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 38 deletions.
10 changes: 7 additions & 3 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ pub struct CliArgs {
)]
pub auth: Vec<auth::RequiredAuth>,

/// Generate a random 6-hexdigit route
#[structopt(long = "random-route")]
pub random_route: bool,
/// Use a specific route prefix
#[structopt(long = "route-prefix")]
pub route_prefix: Option<String>,

/// Use a random route prefix
#[structopt(long = "random-route-prefix", conflicts_with("route-prefix"))]
pub random_route_prefix: bool,

/// Do not follow symbolic links
#[structopt(short = "P", long = "no-symlinks")]
Expand Down
14 changes: 4 additions & 10 deletions src/listing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ pub fn directory_listing(
}

let base = Path::new(serve_path);
let random_route_abs = format!("/{}", conf.random_route.clone().unwrap_or_default());
let is_root = base.parent().is_none() || Path::new(&req.path()) == Path::new(&random_route_abs);
let route_prefix_abs = format!("{}/", &conf.route_prefix);
let is_root = base.parent().is_none() || Path::new(&req.path()) == Path::new(&route_prefix_abs);

let encoded_dir = match base.strip_prefix(random_route_abs) {
let encoded_dir = match base.strip_prefix(route_prefix_abs) {
Ok(c_d) => Path::new("/").join(c_d),
Err(_) => base.to_path_buf(),
}
Expand All @@ -191,13 +191,7 @@ pub fn directory_listing(
let decoded = percent_decode_str(&encoded_dir).decode_utf8_lossy();

let mut res: Vec<Breadcrumb> = Vec::new();
let mut link_accumulator = format!(
"/{}",
conf.random_route
.clone()
.map(|r| r + "/")
.unwrap_or_default()
);
let mut link_accumulator = format!("{}/", &conf.route_prefix);

let mut components = Path::new(&*decoded).components().peekable();

Expand Down
31 changes: 12 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ pub struct MiniserveConfig {
/// Show hidden files
pub show_hidden: bool,

/// Enable random route generation
pub random_route: Option<String>,
/// Route prefix; Either empty (default) or contains a route with a leading slash
pub route_prefix: String,

/// Randomly generated favicon route
pub favicon_route: String,
Expand Down Expand Up @@ -121,10 +121,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_prefix) {
(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
Expand All @@ -151,7 +152,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,
Expand Down Expand Up @@ -301,21 +302,13 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> {
"{}",
Color::Green
.paint(format!(
"http://{interface}:{port}",
"http://{interface}:{port}{prefix}",
interface = &interface,
port = miniserve_config.port
port = miniserve_config.port,
prefix = miniserve_config.route_prefix,
))
.bold()
));

if let Some(random_route) = miniserve_config.clone().random_route {
addresses.push_str(&format!(
"{}",
Color::Green
.paint(format!("/{random_route}", random_route = random_route,))
.bold()
));
}
}

let socket_addresses = interfaces
Expand Down Expand Up @@ -359,7 +352,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)
.configure(|c| configure_app(c, &inside_config)),
)
.default_service(web::get().to(error_404))
Expand Down
9 changes: 3 additions & 6 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ pub fn page(
encoded_dir: &str,
conf: &MiniserveConfig,
) -> Markup {
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);

Expand Down Expand Up @@ -481,8 +478,8 @@ pub fn render_error(
@for error in error_description.lines() {
p { (error) }
}
// WARN don't expose random route!
@if !conf.random_route.is_some() {
// WARN don't expose route prefix!
@if conf.route_prefix.is_empty() {
div.error-nav {
a.error-back href=(return_address) {
"Go back to file listing"
Expand Down

0 comments on commit 7ea3beb

Please sign in to comment.