Skip to content

Commit 10c9524

Browse files
authored
fix: restructure GroupRouter to use PlatformData (#257)
1 parent 2093c60 commit 10c9524

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

crates/shared/src/core/engine.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,20 @@ use crate::{
99
Middleware, NgynMiddleware,
1010
};
1111

12-
pub struct GroupRouter<'b> {
13-
base_path: &'b str,
14-
router: Router<RouteHandler>,
12+
#[derive(Default)]
13+
pub struct GroupRouter {
14+
data: PlatformData,
1515
}
1616

17-
impl RouteInstance for GroupRouter<'_> {
18-
fn router_mut(&mut self) -> &mut Router<RouteHandler> {
19-
&mut self.router
20-
}
21-
22-
fn mount(&self) -> &str {
23-
self.base_path
17+
impl NgynPlatform for GroupRouter {
18+
fn data_mut(&mut self) -> &mut PlatformData {
19+
&mut self.data
2420
}
2521
}
2622

2723
#[derive(Default)]
2824
pub struct PlatformData {
25+
base_path: &'static str,
2926
router: Router<RouteHandler>,
3027
middlewares: Vec<Box<dyn crate::Middleware>>,
3128
state: Option<Arc<Box<dyn AppState>>>,
@@ -102,7 +99,7 @@ pub trait RouteInstance {
10299
fn router_mut(&mut self) -> &mut Router<RouteHandler>;
103100

104101
/// Mounts the route on a path, defaults to "/"
105-
fn mount(&self) -> &str {
102+
fn mount(&mut self) -> &str {
106103
"/"
107104
}
108105

@@ -215,13 +212,15 @@ pub trait NgynEngine: NgynPlatform {
215212
}
216213

217214
/// Groups related routes
218-
fn group(&mut self, base_path: &str, registry: impl Fn(&mut GroupRouter)) {
215+
fn group(&mut self, base_path: &'static str, registry: impl Fn(&mut GroupRouter)) {
219216
let mut group = GroupRouter {
220-
base_path,
221-
router: Router::<RouteHandler>::new(),
217+
data: PlatformData {
218+
base_path,
219+
..Default::default()
220+
},
222221
};
223222
registry(&mut group);
224-
self.data_mut().router.merge(group.router).unwrap();
223+
self.data_mut().router.merge(group.data.router).unwrap();
225224
}
226225

227226
/// Adds a middleware to the application.
@@ -254,6 +253,10 @@ impl<T: NgynPlatform> RouteInstance for T {
254253
fn router_mut(&mut self) -> &mut Router<RouteHandler> {
255254
&mut self.data_mut().router
256255
}
256+
257+
fn mount(&mut self) -> &str {
258+
self.data_mut().base_path
259+
}
257260
}
258261
impl<T: NgynHttpPlatform> NgynHttpEngine for T {}
259262

0 commit comments

Comments
 (0)