@@ -2,6 +2,7 @@ use std::future::Future;
2
2
use std:: pin:: Pin ;
3
3
use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
4
4
use std:: sync:: Arc ;
5
+ use tide:: http:: mime;
5
6
use tide:: { After , Before , Middleware , Next , Request , Response , Result , StatusCode } ;
6
7
7
8
#[ derive( Debug ) ]
@@ -70,10 +71,10 @@ impl<State: Send + Sync + 'static> Middleware<State> for RequestCounterMiddlewar
70
71
tide:: log:: trace!( "request counter" , { count: count } ) ;
71
72
req. set_ext ( RequestCount ( count) ) ;
72
73
73
- let mut response = next. run ( req) . await ?;
74
+ let mut res = next. run ( req) . await ?;
74
75
75
- response = response . set_header ( "request-number" , count. to_string ( ) ) ;
76
- Ok ( response )
76
+ res . insert_header ( "request-number" , count. to_string ( ) ) ;
77
+ Ok ( res )
77
78
} )
78
79
}
79
80
}
@@ -101,14 +102,18 @@ async fn main() -> Result<()> {
101
102
app. middleware ( After ( |result : Result | async move {
102
103
let response = result. unwrap_or_else ( |e| Response :: new ( e. status ( ) ) ) ;
103
104
match response. status ( ) {
104
- StatusCode :: NotFound => Ok ( response
105
- . set_content_type ( tide:: http:: mime:: HTML )
106
- . body_string ( NOT_FOUND_HTML_PAGE . into ( ) ) ) ,
107
-
108
- StatusCode :: InternalServerError => Ok ( response
109
- . set_content_type ( tide:: http:: mime:: HTML )
110
- . body_string ( INTERNAL_SERVER_ERROR_HTML_PAGE . into ( ) ) ) ,
111
-
105
+ StatusCode :: NotFound => {
106
+ let mut res = Response :: new ( 404 ) ;
107
+ res. set_content_type ( mime:: HTML ) ;
108
+ res. set_body ( NOT_FOUND_HTML_PAGE ) ;
109
+ Ok ( res)
110
+ }
111
+ StatusCode :: InternalServerError => {
112
+ let mut res = Response :: new ( 500 ) ;
113
+ res. set_content_type ( mime:: HTML ) ;
114
+ res. set_body ( INTERNAL_SERVER_ERROR_HTML_PAGE ) ;
115
+ Ok ( res)
116
+ }
112
117
_ => Ok ( response) ,
113
118
}
114
119
} ) ) ;
0 commit comments