11pub mod cors;
22pub mod gzip;
33
4- use anyhow:: { Error , Result } ;
4+ use anyhow:: Error ;
55use futures:: Future ;
66use http:: { Request , Response } ;
77use hyper:: Body ;
@@ -15,15 +15,34 @@ use crate::config::Config;
1515use self :: cors:: make_cors_middleware;
1616use self :: gzip:: make_gzip_compression_middleware;
1717
18- pub type MiddlewareBefore = Box < dyn Fn ( & mut Request < Body > ) + Send + Sync > ;
18+ /// Middleware chain `Result` which specifies the `Err` variant as a
19+ /// HTTP response.
20+ pub type Result = std:: result:: Result < ( ) , Response < Body > > ;
21+
22+ /// Middleware chain to execute before the main handler digests the
23+ /// HTTP request. No HTTP response is available at this point.
24+ pub type MiddlewareBefore = Box <
25+ dyn Fn ( & mut Request < Body > ) -> Pin < Box < dyn Future < Output = Result > + Send + Sync > > + Send + Sync ,
26+ > ;
27+
28+ /// Middleware chain to execute after the main handler digests the
29+ /// HTTP request. The HTTP response is created by the handler and
30+ /// consumed by every middleware after chain.
1931pub type MiddlewareAfter = Box <
2032 dyn Fn (
2133 Arc < Request < Body > > ,
2234 Arc < Mutex < Response < Body > > > ,
23- ) -> Pin < Box < dyn Future < Output = Result < ( ) > > + Send + Sync > >
35+ ) -> Pin < Box < dyn Future < Output = Result > + Send + Sync > >
2436 + Send
2537 + Sync ,
2638> ;
39+
40+ /// The main handler for the HTTP request, a HTTP response is created
41+ /// as a result of this handler.
42+ ///
43+ /// This handler will be executed against the HTTP request after every
44+ /// "Middleware Before" chain is executed but before any "Middleware After"
45+ /// chain is executed
2746pub type Handler = Box <
2847 dyn Fn ( Arc < Request < Body > > ) -> Pin < Box < dyn Future < Output = Response < Body > > + Send + Sync > >
2948 + Send
@@ -57,16 +76,18 @@ impl Middleware {
5776 /// with the HTTP Response from the handler
5877 pub async fn handle ( & self , mut request : Request < Body > , handler : Handler ) -> Response < Body > {
5978 for fx in self . before . iter ( ) {
60- fx ( & mut request) ;
79+ if let Err ( err) = fx ( & mut request) . await {
80+ return err;
81+ }
6182 }
6283
6384 let request = Arc :: new ( request) ;
6485 let response = handler ( Arc :: clone ( & request) ) . await ;
6586 let response = Arc :: new ( Mutex :: new ( response) ) ;
6687
6788 for fx in self . after . iter ( ) {
68- if let Err ( error ) = fx ( Arc :: clone ( & request) , Arc :: clone ( & response) ) . await {
69- eprintln ! ( "{:?}" , error ) ;
89+ if let Err ( err ) = fx ( Arc :: clone ( & request) , Arc :: clone ( & response) ) . await {
90+ return err ;
7091 }
7192 }
7293
@@ -88,7 +109,7 @@ impl Default for Middleware {
88109impl TryFrom < Arc < Config > > for Middleware {
89110 type Error = Error ;
90111
91- fn try_from ( config : Arc < Config > ) -> Result < Self , Self :: Error > {
112+ fn try_from ( config : Arc < Config > ) -> std :: result :: Result < Self , Self :: Error > {
92113 let mut middleware = Middleware :: default ( ) ;
93114
94115 if let Some ( cors_config) = config. cors ( ) {
0 commit comments