Skip to content

Commit 681f9a3

Browse files
committed
Add the ability to log request headers in production.
There are enough different IPs requesting large number of crate details endpoints at the same time that I think we have a page making more API calls than it should be. I'd also like the see the UA header for a crawler hitting us a lot. This enables me to do both.
1 parent 0addcb1 commit 681f9a3

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

src/middleware/debug.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,9 @@ pub struct Debug;
77

88
impl Middleware for Debug {
99
fn before(&self, req: &mut Request) -> Result<(), Box<Error + Send>> {
10-
println!(" version: {}", req.http_version());
11-
println!(" method: {:?}", req.method());
12-
println!(" scheme: {:?}", req.scheme());
13-
println!(" host: {:?}", req.host());
14-
println!(" path: {}", req.path());
15-
println!(" query_string: {:?}", req.query_string());
16-
println!(" remote_addr: {:?}", req.remote_addr());
17-
for &(k, ref v) in &req.headers().all() {
18-
println!(" hdr: {}={:?}", k, v);
19-
}
20-
Ok(())
10+
DebugRequest.before(req)
2111
}
12+
2213
fn after(
2314
&self,
2415
_req: &mut Request,
@@ -33,3 +24,22 @@ impl Middleware for Debug {
3324
})
3425
}
3526
}
27+
28+
#[derive(Clone, Copy, Debug)]
29+
pub struct DebugRequest;
30+
31+
impl Middleware for DebugRequest{
32+
fn before(&self, req: &mut Request) -> Result<(), Box<Error + Send>> {
33+
println!(" version: {}", req.http_version());
34+
println!(" method: {:?}", req.method());
35+
println!(" scheme: {:?}", req.scheme());
36+
println!(" host: {:?}", req.host());
37+
println!(" path: {}", req.path());
38+
println!(" query_string: {:?}", req.query_string());
39+
println!(" remote_addr: {:?}", req.remote_addr());
40+
for &(k, ref v) in &req.headers().all() {
41+
println!(" hdr: {}={:?}", k, v);
42+
}
43+
Ok(())
44+
}
45+
}

src/middleware/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod prelude {
66

77
pub use self::app::AppMiddleware;
88
pub use self::current_user::CurrentUser;
9-
pub use self::debug::Debug;
9+
pub use self::debug::*;
1010
pub use self::ember_index_rewrite::EmberIndexRewrite;
1111
pub use self::head::Head;
1212
pub use self::security_headers::SecurityHeaders;
@@ -25,6 +25,7 @@ use conduit_conditional_get::ConditionalGet;
2525
use conduit_cookie::{Middleware as Cookie, SessionMiddleware};
2626
use conduit_log_requests::LogRequests;
2727

28+
use std::env;
2829
use std::sync::Arc;
2930
use cookie;
3031
use log;
@@ -43,6 +44,10 @@ pub fn build_middleware(app: Arc<App>, endpoints: R404) -> MiddlewareBuilder {
4344
m.around(StaticOrContinue::new("local_uploads"));
4445
}
4546

47+
if env::var_os("DEBUG_REQUESTS").is_some() {
48+
m.add(DebugRequest);
49+
}
50+
4651
if env != Env::Test {
4752
m.add(LogRequests(log::LogLevel::Info));
4853
}

0 commit comments

Comments
 (0)