From 905b70349bbd02a31779a64dcb2c6553051cd880 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 14 Jan 2024 14:52:19 +0800 Subject: [PATCH] :sparkles: Clean url with `suffix` --- regions/index.toml | 2 +- regions/subfolder/index.html | 15 +++++++++++++++ src/proxies/config.rs | 11 +++++++---- src/proxies/loader.rs | 4 +++- src/proxies/responder.rs | 19 ++++++++++++++++++- 5 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 regions/subfolder/index.html diff --git a/regions/index.toml b/regions/index.toml index 22e2901..db75202 100644 --- a/regions/index.toml +++ b/regions/index.toml @@ -6,4 +6,4 @@ hosts = ["localhost"] paths = ["/"] [[locations.destinations]] id = "static" -uri = "files://regions/index.html" +uri = "files://regions?index=index.html" diff --git a/regions/subfolder/index.html b/regions/subfolder/index.html new file mode 100644 index 0000000..9890e97 --- /dev/null +++ b/regions/subfolder/index.html @@ -0,0 +1,15 @@ + + + + + + Hello, World! + + +

Hello, there!

+

+ Here's the roadsign benchmarking test data! And you are in the subfolder + now! +

+ + diff --git a/src/proxies/config.rs b/src/proxies/config.rs index 552f54a..c6c1835 100644 --- a/src/proxies/config.rs +++ b/src/proxies/config.rs @@ -98,20 +98,23 @@ impl Destination { .get("utf8") .and_then(|val| val.as_bool()) .unwrap_or(false), - with_slash: queries - .get("slash") - .and_then(|val| val.as_bool()) - .unwrap_or(false), browse: queries .get("browse") .and_then(|val| val.as_bool()) .unwrap_or(false), + with_slash: queries + .get("slash") + .and_then(|val| val.as_bool()) + .unwrap_or(false), index: queries .get("index") .and_then(|val| val.as_str().map(str::to_string)), fallback: queries .get("fallback") .and_then(|val| val.as_str().map(str::to_string)), + suffix: queries + .get("suffix") + .and_then(|val| val.as_str().map(str::to_string)), }) } _ => Err(()), diff --git a/src/proxies/loader.rs b/src/proxies/loader.rs index 0928e00..d9bbb95 100644 --- a/src/proxies/loader.rs +++ b/src/proxies/loader.rs @@ -20,7 +20,9 @@ pub fn scan_regions(basepath: String) -> io::Result<(Vec, u32)> } pub fn load_region(file: DirEntry) -> Result { - if file.path().extension().and_then(OsStr::to_str).unwrap() != "toml" { + if file.metadata().map(|val| val.is_dir()).unwrap() + || file.path().extension().and_then(OsStr::to_str).unwrap() != "toml" + { return Err("File entry wasn't toml file".to_string()); } diff --git a/src/proxies/responder.rs b/src/proxies/responder.rs index f9fbeac..fa60aff 100644 --- a/src/proxies/responder.rs +++ b/src/proxies/responder.rs @@ -97,10 +97,11 @@ pub async fn respond_hypertext( pub struct StaticResponderConfig { pub uri: String, pub utf8: bool, - pub with_slash: bool, pub browse: bool, + pub with_slash: bool, pub index: Option, pub fallback: Option, + pub suffix: Option, } pub async fn respond_static( @@ -142,6 +143,22 @@ pub async fn respond_static( } if !file_path.exists() { + if let Some(suffix) = cfg.suffix { + let file_name = file_path + .file_name() + .and_then(OsStr::to_str) + .unwrap() + .to_string(); + file_path.pop(); + file_path.push((file_name + &suffix).as_str()); + if file_path.is_file() { + return Ok(StaticFileRequest::from_request_without_body(req) + .await? + .create_response(&file_path, cfg.utf8)? + .into_response()); + } + } + if let Some(file) = cfg.fallback { let fallback_path = base_path.join(file); if fallback_path.is_file() {