use std::collections::HashSet;
use http::HeaderName;
use tower_layer::Layer;
use crate::service::DebugService;
#[derive(Debug, Clone)]
pub struct DebugLayer {
config: DebugConfig,
}
#[derive(Debug, Clone)]
pub struct DebugConfig {
pub log_headers: bool,
pub log_bodies: bool,
pub log_response_frames: bool,
pub max_body_bytes: usize,
pub hex_dump: bool,
pub sensitive_headers: HashSet<HeaderName>,
pub reveal_sensitive_headers: bool,
}
impl Default for DebugConfig {
fn default() -> Self {
Self {
log_headers: true,
log_bodies: true,
log_response_frames: true,
max_body_bytes: 4096,
hex_dump: false,
sensitive_headers: HashSet::from([HeaderName::from_static("authorization")]),
reveal_sensitive_headers: false,
}
}
}
impl DebugLayer {
pub fn new() -> Self {
Self {
config: DebugConfig::default(),
}
}
pub fn with_config(config: DebugConfig) -> Self {
Self { config }
}
pub fn sensitive_headers(mut self, sensitive_headers: HashSet<HeaderName>) -> Self {
self.config.sensitive_headers = sensitive_headers;
self
}
pub fn reveal_sensitive_headers(mut self, enabled: bool) -> Self {
self.config.reveal_sensitive_headers = enabled;
self
}
pub fn log_headers(mut self, enabled: bool) -> Self {
self.config.log_headers = enabled;
self
}
pub fn log_bodies(mut self, enabled: bool) -> Self {
self.config.log_bodies = enabled;
self
}
pub fn log_response_frames(mut self, enabled: bool) -> Self {
self.config.log_response_frames = enabled;
self
}
pub fn max_body_bytes(mut self, max: usize) -> Self {
self.config.max_body_bytes = max;
self
}
pub fn hex_dump(mut self, enabled: bool) -> Self {
self.config.hex_dump = enabled;
self
}
}
impl Default for DebugLayer {
fn default() -> Self {
Self::new()
}
}
impl<S> Layer<S> for DebugLayer {
type Service = DebugService<S>;
fn layer(&self, inner: S) -> Self::Service {
DebugService::new(inner, self.config.clone())
}
}