Skip to content

Commit

Permalink
feat: 开放RootRoute, 引入root_middleware概念, Route添加root_hook相关方法
Browse files Browse the repository at this point in the history
  • Loading branch information
hubertshelley committed Jul 2, 2024
1 parent 2df35e2 commit 7db89c8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion silent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion silent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
12 changes: 12 additions & 0 deletions silent/src/route/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct Route {
pub handler: HashMap<Method, Arc<dyn Handler>>,
pub children: Vec<Route>,
pub middlewares: Vec<Arc<dyn MiddleWareHandler>>,
pub root_middlewares: Vec<Arc<dyn MiddleWareHandler>>,
special_match: bool,
create_path: String,
}
Expand Down Expand Up @@ -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(),
};
Expand All @@ -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
}
Expand All @@ -110,6 +113,7 @@ impl Route {
}
pub fn append<R: RouterAdapt>(mut self, route: R) -> Self {
let mut route = route.into_router();
self.root_middlewares.extend(route.root_middlewares.clone());
self.middlewares
.iter()
.cloned()
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions silent/src/route/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 7db89c8

Please sign in to comment.