aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFuwn <[email protected]>2022-04-07 03:26:00 +0000
committerFuwn <[email protected]>2022-04-07 03:26:00 +0000
commit4fb648925985714bb75ec656629f771207ca30c5 (patch)
tree19dc69704c12940a66ab5749195a043b1fe107a3 /src
parentrefactor(examples): bind router (diff)
downloadwindmark-4fb648925985714bb75ec656629f771207ca30c5.tar.xz
windmark-4fb648925985714bb75ec656629f771207ca30c5.zip
feat(router): allow path fixing
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 671ae9b..b9024c4 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -156,6 +156,7 @@ pub struct Router {
language: String,
port: i32,
modules: Arc<Mutex<Vec<Box<dyn Module + Send>>>>,
+ fix_path: bool,
}
impl Router {
/// Create a new `Router`
@@ -361,7 +362,12 @@ impl Router {
}
}
- let route = &mut self.routes.at(url.path());
+ let fixed_path = if self.fix_path {
+ self.routes.fix_path(url.path()).unwrap()
+ } else {
+ url.path().to_string()
+ };
+ let route = &mut self.routes.at(&fixed_path);
for module in &mut *self.modules.lock().unwrap() {
module.on_pre_route(CallbackContext::new(stream.get_ref(), &url, {
@@ -787,6 +793,20 @@ impl Router {
self
}
+
+ /// Performs a case-insensitive lookup of routes, using the case corrected
+ /// path if successful. Missing/ extra trailing slashes are also corrected.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// windmark::Router::new().set_fix_path(true);
+ /// ```
+ pub fn set_fix_path(&mut self, fix_path: bool) -> &mut Self {
+ self.fix_path = fix_path;
+
+ self
+ }
}
impl Default for Router {
fn default() -> Self {
@@ -814,6 +834,7 @@ impl Default for Router {
language: "en".to_string(),
port: 1965,
modules: Arc::new(Mutex::new(vec![])),
+ fix_path: false,
}
}
}