Skip to content

Commit

Permalink
refactor: matching url as utils
Browse files Browse the repository at this point in the history
Signed-off-by: David Dal Busco <[email protected]>
  • Loading branch information
peterpeterparker committed Nov 9, 2023
1 parent 4255b94 commit 5264273
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/satellite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::storage::store::{
init_certified_assets, list_assets as list_assets_store, set_config as set_storage_config,
set_domain,
};
use crate::storage::types::config::StorageConfigRewrites;
use crate::storage::types::domain::{CustomDomains, DomainName};
use crate::storage::types::http_request::{
Routing, RoutingDefault, RoutingRedirect, RoutingRewrite,
Expand Down Expand Up @@ -65,7 +66,6 @@ use storage::http::types::{
HttpRequest, HttpResponse, StreamingCallbackHttpResponse, StreamingCallbackToken,
};
use types::list::ListParams;
use crate::storage::types::config::StorageConfigRewrites;

#[init]
fn init() {
Expand Down
28 changes: 8 additions & 20 deletions src/satellite/src/storage/rewrites.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::storage::constants::ROOT_PATHS;
use crate::storage::types::config::{StorageConfig, StorageConfigRedirect};
use crate::storage::url::separator;
use globset::Glob;
use crate::storage::url::{matching_urls, separator};
use regex::Regex;
use std::cmp::Ordering;
use std::collections::HashMap;
Expand All @@ -13,7 +12,7 @@ pub fn rewrite_url(requested_path: &str, config: &StorageConfig) -> Option<(Stri
redirects: _,
} = config;

let matches = matching_urls(requested_path, rewrites);
let matches = matching_rewrite_urls(requested_path, rewrites);

matches
.first()
Expand All @@ -38,27 +37,16 @@ pub fn is_root_path(path: &str) -> bool {
pub fn redirect_url(requested_path: &str, config: &StorageConfig) -> Option<StorageConfigRedirect> {
let redirects = config.unwrap_redirects();

let matches = matching_urls(requested_path, &redirects);
let matches = matching_rewrite_urls(requested_path, &redirects);

matches.first().map(|(_, destination)| destination.clone())
}

fn matching_urls<T: Clone>(requested_path: &str, config: &HashMap<String, T>) -> Vec<(String, T)> {
let mut matches: Vec<(String, T)> = config
.iter()
.filter(|(source, _)| {
let glob = Glob::new(source);

match glob {
Err(_) => false,
Ok(glob) => {
let matcher = glob.compile_matcher();
matcher.is_match(requested_path)
}
}
})
.map(|(source, destination)| (source.clone(), destination.clone()))
.collect();
fn matching_rewrite_urls<T: Clone>(
requested_path: &str,
config: &HashMap<String, T>,
) -> Vec<(String, T)> {
let mut matches: Vec<(String, T)> = matching_urls(requested_path, config);

matches.sort_by(|(a, _), (b, _)| {
let a_parts: Vec<&str> = a.split('/').collect();
Expand Down
23 changes: 23 additions & 0 deletions src/satellite/src/storage/url.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::storage::types::http_request::MapUrl;
use crate::storage::types::state::FullPath;
use globset::Glob;
use std::collections::HashMap;
use std::path::Path;
use url::{ParseError, Url};

Expand Down Expand Up @@ -123,3 +125,24 @@ fn map_token(parsed_url: Url) -> Option<String> {

None
}

pub fn matching_urls<T: Clone>(
requested_path: &str,
config: &HashMap<String, T>,
) -> Vec<(String, T)> {
config
.iter()
.filter(|(source, _)| {
let glob = Glob::new(source);

match glob {
Err(_) => false,
Ok(glob) => {
let matcher = glob.compile_matcher();
matcher.is_match(requested_path)
}
}
})
.map(|(source, destination)| (source.clone(), destination.clone()))
.collect()
}

0 comments on commit 5264273

Please sign in to comment.