wisegate_core/
default_config.rs1use crate::auth::Credentials;
26use crate::defaults;
27use crate::types::{
28 AuthenticationProvider, ConnectionProvider, FilteringProvider, ProxyConfig, ProxyProvider,
29 RateLimitCleanupConfig, RateLimitConfig, RateLimitingProvider,
30};
31
32#[derive(Debug, Clone)]
39pub struct DefaultConfig {
40 pub rate_limit: RateLimitConfig,
42 pub cleanup: RateLimitCleanupConfig,
44 pub proxy: ProxyConfig,
46 pub allowed_proxy_ips: Option<Vec<String>>,
48 pub blocked_ips: Vec<String>,
50 pub blocked_methods: Vec<String>,
52 pub blocked_patterns: Vec<String>,
54 pub max_connections: usize,
56 pub auth_credentials: Credentials,
58 pub auth_realm: String,
60 pub bearer_token: Option<String>,
62 pub forward_authorization_header: bool,
64}
65
66impl Default for DefaultConfig {
67 fn default() -> Self {
68 Self {
69 rate_limit: RateLimitConfig {
70 max_requests: defaults::RATE_LIMIT_REQUESTS,
71 window_duration: defaults::RATE_LIMIT_WINDOW,
72 },
73 cleanup: RateLimitCleanupConfig {
74 threshold: defaults::RATE_LIMIT_CLEANUP_THRESHOLD,
75 interval: defaults::RATE_LIMIT_CLEANUP_INTERVAL,
76 },
77 proxy: ProxyConfig {
78 timeout: defaults::PROXY_TIMEOUT,
79 max_body_size: defaults::MAX_BODY_SIZE,
80 },
81 allowed_proxy_ips: None,
82 blocked_ips: Vec::new(),
83 blocked_methods: Vec::new(),
84 blocked_patterns: Vec::new(),
85 max_connections: defaults::MAX_CONNECTIONS,
86 auth_credentials: Credentials::new(),
87 auth_realm: defaults::AUTH_REALM.to_string(),
88 bearer_token: None,
89 forward_authorization_header: false,
90 }
91 }
92}
93
94impl RateLimitingProvider for DefaultConfig {
95 fn rate_limit_config(&self) -> &RateLimitConfig {
96 &self.rate_limit
97 }
98 fn rate_limit_cleanup_config(&self) -> &RateLimitCleanupConfig {
99 &self.cleanup
100 }
101}
102
103impl ProxyProvider for DefaultConfig {
104 fn proxy_config(&self) -> &ProxyConfig {
105 &self.proxy
106 }
107 fn allowed_proxy_ips(&self) -> Option<&[String]> {
108 self.allowed_proxy_ips.as_deref()
109 }
110}
111
112impl FilteringProvider for DefaultConfig {
113 fn blocked_ips(&self) -> &[String] {
114 &self.blocked_ips
115 }
116 fn blocked_methods(&self) -> &[String] {
117 &self.blocked_methods
118 }
119 fn blocked_patterns(&self) -> &[String] {
120 &self.blocked_patterns
121 }
122}
123
124impl ConnectionProvider for DefaultConfig {
125 fn max_connections(&self) -> usize {
126 self.max_connections
127 }
128}
129
130impl AuthenticationProvider for DefaultConfig {
131 fn auth_credentials(&self) -> &Credentials {
132 &self.auth_credentials
133 }
134 fn auth_realm(&self) -> &str {
135 &self.auth_realm
136 }
137 fn bearer_token(&self) -> Option<&str> {
138 self.bearer_token.as_deref()
139 }
140 fn forward_authorization_header(&self) -> bool {
141 self.forward_authorization_header
142 }
143}
144
145#[cfg(test)]
146mod tests {
147 use super::*;
148 use crate::types::ConfigProvider;
149
150 fn assert_is_config_provider<T: ConfigProvider>(_: &T) {}
151
152 #[test]
153 fn default_config_implements_config_provider() {
154 let config = DefaultConfig::default();
155 assert_is_config_provider(&config);
156 }
157
158 #[test]
159 fn default_values_match_cli_defaults() {
160 let config = DefaultConfig::default();
161 assert_eq!(
162 config.rate_limit.max_requests,
163 defaults::RATE_LIMIT_REQUESTS
164 );
165 assert_eq!(config.max_connections, defaults::MAX_CONNECTIONS);
166 assert!(config.allowed_proxy_ips.is_none());
167 assert!(!config.forward_authorization_header);
168 }
169}