Skip to content

Commit f6cf725

Browse files
committed
chore: refactor to app and clone handler down to router
1 parent 59897f8 commit f6cf725

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
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
@@ -54,8 +59,8 @@ impl<Data: Clone + Send + Sync + 'static> App<Data> {
5459
}
5560

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

@@ -70,6 +75,7 @@ impl<Data: Clone + Send + Sync + 'static> App<Data> {
7075
Server {
7176
data: self.data,
7277
router: Arc::new(self.router),
78+
default_handler: Arc::clone(&self.default_handler),
7379
}
7480
}
7581

@@ -101,6 +107,7 @@ impl<Data: Clone + Send + Sync + 'static> App<Data> {
101107
struct Server<Data> {
102108
data: Data,
103109
router: Arc<Router<Data>>,
110+
default_handler: Arc<BoxedEndpoint<Data>>,
104111
}
105112

106113
impl<Data: Clone + Send + Sync + 'static> Service for Server<Data> {
@@ -112,6 +119,7 @@ impl<Data: Clone + Send + Sync + 'static> Service for Server<Data> {
112119
fn call(&mut self, req: http::Request<hyper::Body>) -> Self::Future {
113120
let data = self.data.clone();
114121
let router = self.router.clone();
122+
let default_handler = self.default_handler.clone();
115123

116124
let req = req.map(Body::from);
117125
let path = req.uri().path().to_owned();
@@ -123,7 +131,7 @@ impl<Data: Clone + Send + Sync + 'static> Service for Server<Data> {
123131
endpoint,
124132
params,
125133
middleware,
126-
} = router.route(&path, &method);
134+
} = router.route(&path, &method, &default_handler);
127135

128136
let ctx = RequestContext {
129137
app_data: data,

src/head.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl<T: Send + 'static + std::str::FromStr, S: 'static> Extract<S> for Path<T> {
8585
Ok(t) => future::ok(Path(t)),
8686
Err(_) => future::err(http::status::StatusCode::BAD_REQUEST.into_response()),
8787
},
88-
None => future::err(http::status::StatusCode::BAD_REQUEST.into_response()),
88+
None => future::err(http::status::StatusCode::INTERNAL_SERVER_ERROR.into_response()),
8989
}
9090
}
9191
}

src/router.rs

Lines changed: 12 additions & 23 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,
@@ -99,7 +94,6 @@ impl<Data: Clone + Send + Sync + 'static> Router<Data> {
9994
Resource {
10095
table,
10196
middleware_base: &self.middleware_base,
102-
default_handler: &self.default_handler,
10397
}
10498
}
10599

@@ -148,26 +142,17 @@ impl<Data: Clone + Send + Sync + 'static> Router<Data> {
148142
self
149143
}
150144

151-
pub fn set_default_handler<T: Endpoint<Data, U>, U>(&mut self, ep: T) -> &mut Self {
152-
self.default_handler = Arc::new(BoxedEndpoint::new(ep));
153-
self
154-
}
155-
156145
pub(crate) fn route<'a>(
157146
&'a self,
158147
path: &'a str,
159148
method: &http::Method,
149+
default_handler: &'a Arc<BoxedEndpoint<Data>>,
160150
) -> RouteResult<'a, Data> {
161151
match self.table.route(path) {
162152
Some((route, route_match)) => route_match_success(route, route_match, method)
163-
.or_else(|| {
164-
Some(route_match_failure(
165-
&self.default_handler,
166-
&self.middleware_base,
167-
))
168-
})
153+
.or_else(|| Some(route_match_failure(default_handler, &self.middleware_base)))
169154
.unwrap(),
170-
None => route_match_failure(&self.default_handler, &self.middleware_base),
155+
None => route_match_failure(default_handler, &self.middleware_base),
171156
}
172157
}
173158
}
@@ -180,7 +165,6 @@ impl<Data: Clone + Send + Sync + 'static> Router<Data> {
180165
pub struct Resource<'a, Data> {
181166
table: &'a mut PathTable<ResourceData<Data>>,
182167
middleware_base: &'a Vec<Arc<dyn Middleware<Data> + Send + Sync>>,
183-
default_handler: &'a Arc<BoxedEndpoint<Data>>,
184168
}
185169

186170
struct ResourceData<Data> {
@@ -200,7 +184,6 @@ impl<'a, Data> Resource<'a, Data> {
200184
let mut subrouter = Router {
201185
table: PathTable::new(),
202186
middleware_base: self.middleware_base.clone(),
203-
default_handler: self.default_handler.clone(),
204187
};
205188
builder(&mut subrouter);
206189
*self.table = subrouter.table;
@@ -289,11 +272,14 @@ mod tests {
289272
path: &'a str,
290273
method: &'a http::Method,
291274
) -> Option<Response> {
275+
let default_handler = Arc::new(BoxedEndpoint::new(async || {
276+
http::status::StatusCode::NOT_FOUND
277+
}));
292278
let RouteResult {
293279
endpoint,
294280
params,
295281
middleware,
296-
} = router.route(path, method);
282+
} = router.route(path, method, &default_handler);
297283

298284
let data = Data::default();
299285
let req = http::Request::builder()
@@ -317,7 +303,10 @@ mod tests {
317303
path: &str,
318304
method: &http::Method,
319305
) -> Option<usize> {
320-
let route_result = router.route(path, method);
306+
let default_handler = Arc::new(BoxedEndpoint::new(async || {
307+
http::status::StatusCode::NOT_FOUND
308+
}));
309+
let route_result = router.route(path, method, &default_handler);
321310
Some(route_result.middleware.len())
322311
}
323312

0 commit comments

Comments
 (0)