Skip to content

Commit 0ff73cf

Browse files
committed
chore: refactor to app and clone handler down to router
1 parent 774af3d commit 0ff73cf

File tree

3 files changed

+24
-28
lines changed

3 files changed

+24
-28
lines changed

src/app.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::{
1111

1212
use crate::{
1313
body::Body,
14+
endpoint::BoxedEndpoint,
1415
endpoint::Endpoint,
1516
extract::Extract,
1617
middleware::{logger::RootLogger, RequestContext},
@@ -26,6 +27,7 @@ use crate::{
2627
pub struct App<Data> {
2728
data: Data,
2829
router: Router<Data>,
30+
default_handler: Arc<BoxedEndpoint<Data>>,
2931
}
3032

3133
impl<Data: Clone + Send + Sync + 'static> App<Data> {
@@ -35,6 +37,9 @@ impl<Data: Clone + Send + Sync + 'static> App<Data> {
3537
let mut app = App {
3638
data,
3739
router: Router::new(),
40+
default_handler: Arc::new(BoxedEndpoint::new(async || {
41+
http::status::StatusCode::NOT_FOUND
42+
})),
3843
};
3944

4045
// Add RootLogger as a default middleware
@@ -53,8 +58,8 @@ impl<Data: Clone + Send + Sync + 'static> App<Data> {
5358
}
5459

5560
/// Set the default handler for the app, a fallback function when there is no match to the route requested
56-
pub fn default_handler<T: Endpoint<Data, U>, U>(&mut self, ep: T) -> &mut Self {
57-
self.router.set_default_handler(ep);
61+
pub fn default_handler<T: Endpoint<Data, U>, U>(&mut self, handler: T) -> &mut Self {
62+
self.default_handler = Arc::new(BoxedEndpoint::new(handler));
5863
self
5964
}
6065

@@ -69,6 +74,7 @@ impl<Data: Clone + Send + Sync + 'static> App<Data> {
6974
Server {
7075
data: self.data,
7176
router: Arc::new(self.router),
77+
default_handler: Arc::clone(&self.default_handler),
7278
}
7379
}
7480

@@ -100,6 +106,7 @@ impl<Data: Clone + Send + Sync + 'static> App<Data> {
100106
struct Server<Data> {
101107
data: Data,
102108
router: Arc<Router<Data>>,
109+
default_handler: Arc<BoxedEndpoint<Data>>,
103110
}
104111

105112
impl<Data: Clone + Send + Sync + 'static> Service for Server<Data> {
@@ -111,6 +118,7 @@ impl<Data: Clone + Send + Sync + 'static> Service for Server<Data> {
111118
fn call(&mut self, req: http::Request<hyper::Body>) -> Self::Future {
112119
let data = self.data.clone();
113120
let router = self.router.clone();
121+
let default_handler = self.default_handler.clone();
114122

115123
let req = req.map(Body::from);
116124
let path = req.uri().path().to_owned();
@@ -122,7 +130,7 @@ impl<Data: Clone + Send + Sync + 'static> Service for Server<Data> {
122130
endpoint,
123131
params,
124132
middleware,
125-
} = router.route(&path, &method);
133+
} = router.route(&path, &method, &default_handler);
126134

127135
let ctx = RequestContext {
128136
app_data: data,

src/head.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl<T: Send + 'static + std::str::FromStr, S: 'static> Extract<S> for Path<T> {
7171
Ok(t) => future::ok(Path(t)),
7272
Err(_) => future::err(http::status::StatusCode::BAD_REQUEST.into_response()),
7373
},
74-
None => future::err(http::status::StatusCode::BAD_REQUEST.into_response()),
74+
None => future::err(http::status::StatusCode::INTERNAL_SERVER_ERROR.into_response()),
7575
}
7676
}
7777
}

src/router.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use path_table::{PathTable, RouteMatch};
1313
pub struct Router<Data> {
1414
table: PathTable<ResourceData<Data>>,
1515
middleware_base: Vec<Arc<dyn Middleware<Data> + Send + Sync>>,
16-
default_handler: Arc<BoxedEndpoint<Data>>,
1716
}
1817

1918
pub(crate) struct RouteResult<'a, Data> {
@@ -22,10 +21,6 @@ pub(crate) struct RouteResult<'a, Data> {
2221
pub(crate) middleware: &'a [Arc<dyn Middleware<Data> + Send + Sync>],
2322
}
2423

25-
pub async fn base_default_handler() -> http::status::StatusCode {
26-
http::status::StatusCode::NOT_FOUND
27-
}
28-
2924
fn route_match_success<'a, Data>(
3025
route: &'a ResourceData<Data>,
3126
route_match: RouteMatch<'a>,
@@ -50,7 +45,7 @@ fn route_match_success<'a, Data>(
5045

5146
fn route_match_failure<'a, Data>(
5247
endpoint: &'a BoxedEndpoint<Data>,
53-
middleware: &'a Vec<Arc<dyn Middleware<Data> + Send + Sync>>,
48+
middleware: &'a [Arc<dyn Middleware<Data> + Send + Sync>],
5449
) -> RouteResult<'a, Data> {
5550
RouteResult {
5651
endpoint,
@@ -65,7 +60,6 @@ impl<Data: Clone + Send + Sync + 'static> Router<Data> {
6560
Router {
6661
table: PathTable::new(),
6762
middleware_base: Vec::new(),
68-
default_handler: Arc::new(BoxedEndpoint::new(base_default_handler)),
6963
}
7064
}
7165

@@ -75,7 +69,6 @@ impl<Data: Clone + Send + Sync + 'static> Router<Data> {
7569
Resource {
7670
table,
7771
middleware_base: &self.middleware_base,
78-
default_handler: &self.default_handler,
7972
}
8073
}
8174

@@ -116,26 +109,17 @@ impl<Data: Clone + Send + Sync + 'static> Router<Data> {
116109
self
117110
}
118111

119-
pub fn set_default_handler<T: Endpoint<Data, U>, U>(&mut self, ep: T) -> &mut Self {
120-
self.default_handler = Arc::new(BoxedEndpoint::new(ep));
121-
self
122-
}
123-
124112
pub(crate) fn route<'a>(
125113
&'a self,
126114
path: &'a str,
127115
method: &http::Method,
116+
default_handler: &'a Arc<BoxedEndpoint<Data>>,
128117
) -> RouteResult<'a, Data> {
129118
match self.table.route(path) {
130119
Some((route, route_match)) => route_match_success(route, route_match, method)
131-
.or_else(|| {
132-
Some(route_match_failure(
133-
&self.default_handler,
134-
&self.middleware_base,
135-
))
136-
})
120+
.or_else(|| Some(route_match_failure(default_handler, &self.middleware_base)))
137121
.unwrap(),
138-
None => route_match_failure(&self.default_handler, &self.middleware_base),
122+
None => route_match_failure(default_handler, &self.middleware_base),
139123
}
140124
}
141125
}
@@ -148,7 +132,6 @@ impl<Data: Clone + Send + Sync + 'static> Router<Data> {
148132
pub struct Resource<'a, Data> {
149133
table: &'a mut PathTable<ResourceData<Data>>,
150134
middleware_base: &'a Vec<Arc<dyn Middleware<Data> + Send + Sync>>,
151-
default_handler: &'a Arc<BoxedEndpoint<Data>>,
152135
}
153136

154137
struct ResourceData<Data> {
@@ -168,7 +151,6 @@ impl<'a, Data> Resource<'a, Data> {
168151
let mut subrouter = Router {
169152
table: PathTable::new(),
170153
middleware_base: self.middleware_base.clone(),
171-
default_handler: self.default_handler.clone(),
172154
};
173155
builder(&mut subrouter);
174156
*self.table = subrouter.table;
@@ -257,11 +239,14 @@ mod tests {
257239
path: &'a str,
258240
method: &'a http::Method,
259241
) -> Option<Response> {
242+
let default_handler = Arc::new(BoxedEndpoint::new(async || {
243+
http::status::StatusCode::NOT_FOUND
244+
}));
260245
let RouteResult {
261246
endpoint,
262247
params,
263248
middleware,
264-
} = router.route(path, method);
249+
} = router.route(path, method, &default_handler);
265250

266251
let data = Data::default();
267252
let req = http::Request::builder()
@@ -285,7 +270,10 @@ mod tests {
285270
path: &str,
286271
method: &http::Method,
287272
) -> Option<usize> {
288-
let route_result = router.route(path, method);
273+
let default_handler = Arc::new(BoxedEndpoint::new(async || {
274+
http::status::StatusCode::NOT_FOUND
275+
}));
276+
let route_result = router.route(path, method, &default_handler);
289277
Some(route_result.middleware.len())
290278
}
291279

0 commit comments

Comments
 (0)