@@ -3,7 +3,9 @@ use crate::{Middleware, Next, Request};
3
3
4
4
/// Log all incoming requests and responses.
5
5
///
6
- /// This middleware is enabled by default in Tide.
6
+ /// This middleware is enabled by default in Tide. In the case of
7
+ /// nested applications, this middleware will only run once for each
8
+ /// request.
7
9
///
8
10
/// # Examples
9
11
///
@@ -16,6 +18,8 @@ pub struct LogMiddleware {
16
18
_priv : ( ) ,
17
19
}
18
20
21
+ struct LogMiddlewareHasBeenRun ;
22
+
19
23
impl LogMiddleware {
20
24
/// Create a new instance of `LogMiddleware`.
21
25
#[ must_use]
@@ -26,17 +30,22 @@ impl LogMiddleware {
26
30
/// Log a request and a response.
27
31
async fn log < ' a , State : Clone + Send + Sync + ' static > (
28
32
& ' a self ,
29
- ctx : Request < State > ,
33
+ mut req : Request < State > ,
30
34
next : Next < ' a , State > ,
31
35
) -> crate :: Result {
32
- let path = ctx. url ( ) . path ( ) . to_owned ( ) ;
33
- let method = ctx. method ( ) . to_string ( ) ;
36
+ if req. ext :: < LogMiddlewareHasBeenRun > ( ) . is_some ( ) {
37
+ return Ok ( next. run ( req) . await ) ;
38
+ }
39
+ req. set_ext ( LogMiddlewareHasBeenRun ) ;
40
+
41
+ let path = req. url ( ) . path ( ) . to_owned ( ) ;
42
+ let method = req. method ( ) . to_string ( ) ;
34
43
log:: info!( "<-- Request received" , {
35
44
method: method,
36
45
path: path,
37
46
} ) ;
38
47
let start = std:: time:: Instant :: now ( ) ;
39
- let response = next. run ( ctx ) . await ;
48
+ let response = next. run ( req ) . await ;
40
49
let status = response. status ( ) ;
41
50
if status. is_server_error ( ) {
42
51
if let Some ( error) = response. error ( ) {
0 commit comments