1use tower_layer::Layer;
7
8use crate::service::DebugService;
9
10#[derive(Debug, Clone)]
12pub struct DebugLayer {
13 config: DebugConfig,
14}
15
16#[derive(Debug, Clone)]
18pub struct DebugConfig {
19 pub log_headers: bool,
21 pub log_bodies: bool,
23 pub log_response_frames: bool,
25 pub max_body_bytes: usize,
27 pub hex_dump: bool,
29}
30
31impl Default for DebugConfig {
32 fn default() -> Self {
33 Self {
34 log_headers: true,
35 log_bodies: true,
36 log_response_frames: true,
37 max_body_bytes: 4096,
38 hex_dump: false,
39 }
40 }
41}
42
43impl DebugLayer {
44 pub fn new() -> Self {
48 Self {
49 config: DebugConfig::default(),
50 }
51 }
52
53 pub fn with_config(config: DebugConfig) -> Self {
55 Self { config }
56 }
57
58 pub fn log_headers(mut self, enabled: bool) -> Self {
60 self.config.log_headers = enabled;
61 self
62 }
63
64 pub fn log_bodies(mut self, enabled: bool) -> Self {
66 self.config.log_bodies = enabled;
67 self
68 }
69
70 pub fn log_response_frames(mut self, enabled: bool) -> Self {
72 self.config.log_response_frames = enabled;
73 self
74 }
75
76 pub fn max_body_bytes(mut self, max: usize) -> Self {
78 self.config.max_body_bytes = max;
79 self
80 }
81
82 pub fn hex_dump(mut self, enabled: bool) -> Self {
84 self.config.hex_dump = enabled;
85 self
86 }
87}
88
89impl Default for DebugLayer {
90 fn default() -> Self {
91 Self::new()
92 }
93}
94
95impl<S> Layer<S> for DebugLayer {
96 type Service = DebugService<S>;
97
98 fn layer(&self, inner: S) -> Self::Service {
99 DebugService::new(inner, self.config.clone())
100 }
101}