@@ -2,14 +2,14 @@ use std::future::Future;
22use std:: pin:: Pin ;
33use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
44use std:: sync:: Arc ;
5- use tide:: { Middleware , Next , Request , Response , Result , StatusCode } ;
5+ use tide:: { After , Before , Middleware , Next , Request , Response , Result , StatusCode } ;
66
77#[ derive( Debug ) ]
88struct User {
99 name : String ,
1010}
1111
12- #[ derive( Default ) ]
12+ #[ derive( Default , Debug ) ]
1313struct UserDatabase ;
1414impl UserDatabase {
1515 async fn find_user ( & self ) -> Option < User > {
@@ -77,14 +77,37 @@ impl<State: Send + Sync + 'static> Middleware<State> for RequestCounterMiddlewar
7777 } )
7878 }
7979}
80+ const NOT_FOUND_HTML_PAGE : & str = "<html><body>
81+ <h1>uh oh, we couldn't find that document</h1>
82+ <p>
83+ probably, this would be served from the file system or
84+ included with `include_bytes!`
85+ </p>
86+ </body></html>" ;
8087
8188#[ async_std:: main]
8289async fn main ( ) -> Result < ( ) > {
8390 tide:: log:: start ( ) ;
8491 let mut app = tide:: with_state ( UserDatabase :: default ( ) ) ;
8592
93+ app. middleware ( After :: new ( |result : Result | async move {
94+ let response = result. unwrap_or_else ( |e| Response :: new ( e. status ( ) ) ) ;
95+ match response. status ( ) {
96+ StatusCode :: NotFound => Ok ( Response :: new ( response. status ( ) )
97+ . set_mime ( tide:: http:: mime:: HTML )
98+ . body_string ( NOT_FOUND_HTML_PAGE . into ( ) ) ) ,
99+ _ => Ok ( response) ,
100+ }
101+ } ) ) ;
102+
86103 app. middleware ( user_loader) ;
87104 app. middleware ( RequestCounterMiddleware :: new ( 0 ) ) ;
105+ app. middleware ( Before :: new (
106+ |mut request : Request < UserDatabase > | async move {
107+ request. set_ext ( std:: time:: Instant :: now ( ) ) ;
108+ request
109+ } ,
110+ ) ) ;
88111
89112 app. at ( "/" ) . get ( |req : Request < _ > | async move {
90113 let count: & RequestCount = req. ext ( ) . unwrap ( ) ;
0 commit comments