From 7db89c8be7faef5ab798c243e7ffbedce0aa808d Mon Sep 17 00:00:00 2001 From: hubertshelley Date: Tue, 2 Jul 2024 09:21:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BC=80=E6=94=BERootRoute,=20?= =?UTF-8?q?=E5=BC=95=E5=85=A5root=5Fmiddleware=E6=A6=82=E5=BF=B5,=20Route?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0root=5Fhook=E7=9B=B8=E5=85=B3=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- silent/Cargo.toml | 2 +- silent/src/lib.rs | 2 +- silent/src/route/mod.rs | 12 ++++++++++++ silent/src/route/root.rs | 1 + 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/silent/Cargo.toml b/silent/Cargo.toml index a29092e..ad8d7fa 100644 --- a/silent/Cargo.toml +++ b/silent/Cargo.toml @@ -41,7 +41,7 @@ tokio = { version = "1.38.0", optional = true } bytes = "1.6.0" http-body-util = "0.1.2" tracing = "0.1.40" -tracing-subscriber = { version = "0.3.18", features = ["local-time"] } +tracing-subscriber = { version = "0.3.18", features = ["chrono"] } async-trait = "0.1.80" serde = { version = "1.0.203", features = ["derive"] } serde_json = "1.0.117" diff --git a/silent/src/lib.rs b/silent/src/lib.rs index f517725..76e35d9 100644 --- a/silent/src/lib.rs +++ b/silent/src/lib.rs @@ -63,7 +63,7 @@ pub mod prelude { #[cfg(feature = "upgrade")] pub use crate::route::handler_append::WSHandlerAppend; pub use crate::route::handler_append::{HandlerAppend, HandlerGetter}; - pub use crate::route::{Route, RouteService, RouterAdapt}; + pub use crate::route::{RootRoute, Route, RouteService, RouterAdapt}; #[cfg(feature = "scheduler")] pub use crate::scheduler::Task; #[cfg(feature = "security")] diff --git a/silent/src/route/mod.rs b/silent/src/route/mod.rs index 43601a7..a282220 100644 --- a/silent/src/route/mod.rs +++ b/silent/src/route/mod.rs @@ -28,6 +28,7 @@ pub struct Route { pub handler: HashMap>, pub children: Vec, pub middlewares: Vec>, + pub root_middlewares: Vec>, special_match: bool, create_path: String, } @@ -80,6 +81,7 @@ impl Route { handler: HashMap::new(), children: Vec::new(), middlewares: Vec::new(), + root_middlewares: Vec::new(), special_match: first_path.starts_with('<') && first_path.ends_with('>'), create_path: path.to_string(), }; @@ -90,6 +92,7 @@ impl Route { } } fn append_route(mut self, route: Route) -> Self { + self.root_middlewares.extend(route.root_middlewares.clone()); self.children.push(route); self } @@ -110,6 +113,7 @@ impl Route { } pub fn append(mut self, route: R) -> Self { let mut route = route.into_router(); + self.root_middlewares.extend(route.root_middlewares.clone()); self.middlewares .iter() .cloned() @@ -118,6 +122,14 @@ impl Route { real_route.children.push(route); self } + pub fn root_hook(mut self, handler: impl MiddleWareHandler + 'static) -> Self { + self.root_middlewares.push(Arc::new(handler)); + self + } + pub fn root_hook_first(mut self, handler: impl MiddleWareHandler + 'static) -> Self { + self.root_middlewares.insert(0, Arc::new(handler)); + self + } pub fn hook(mut self, handler: impl MiddleWareHandler + 'static) -> Self { self.middleware_hook(Arc::new(handler)); self diff --git a/silent/src/route/root.rs b/silent/src/route/root.rs index ef63571..d76c227 100644 --- a/silent/src/route/root.rs +++ b/silent/src/route/root.rs @@ -80,6 +80,7 @@ impl RootRoute { pub fn push(&mut self, route: Route) { self.middlewares.clone_from(&route.middlewares); + self.middlewares.extend(route.root_middlewares.clone()); self.children.push(route); }