attuned_http/
config.rs

1//! Server configuration.
2
3use crate::middleware::{AuthConfig, RateLimitConfig};
4use std::net::SocketAddr;
5use std::time::Duration;
6
7#[cfg(feature = "inference")]
8use attuned_infer::InferenceConfig;
9
10/// Configuration for the HTTP server.
11#[derive(Clone, Debug)]
12pub struct ServerConfig {
13    /// Address to bind the server to.
14    /// Default: 127.0.0.1:8080 (localhost only for security)
15    pub bind_addr: SocketAddr,
16
17    /// Request timeout.
18    /// Default: 30 seconds
19    pub request_timeout: Duration,
20
21    /// Maximum request body size in bytes.
22    /// Default: 1MB
23    pub body_limit: usize,
24
25    /// CORS allowed origins.
26    /// Default: empty (CORS disabled)
27    pub cors_origins: Vec<String>,
28
29    /// API key authentication configuration.
30    /// Default: disabled (no keys configured)
31    pub auth: AuthConfig,
32
33    /// Rate limiting configuration.
34    /// Default: 100 requests per minute per IP
35    pub rate_limit: RateLimitConfig,
36
37    /// Enable security headers.
38    /// Default: true
39    pub security_headers: bool,
40
41    /// Enable automatic inference from message text.
42    /// Default: false (requires "inference" feature)
43    #[cfg(feature = "inference")]
44    pub enable_inference: bool,
45
46    /// Inference engine configuration.
47    /// Default: None (uses InferenceConfig::default())
48    #[cfg(feature = "inference")]
49    pub inference_config: Option<InferenceConfig>,
50}
51
52impl Default for ServerConfig {
53    fn default() -> Self {
54        Self {
55            // Secure default: localhost only
56            bind_addr: "127.0.0.1:8080".parse().unwrap(),
57            request_timeout: Duration::from_secs(30),
58            body_limit: 1024 * 1024, // 1MB
59            cors_origins: vec![],
60            auth: AuthConfig::default(),
61            rate_limit: RateLimitConfig::default(),
62            security_headers: true,
63            #[cfg(feature = "inference")]
64            enable_inference: false,
65            #[cfg(feature = "inference")]
66            inference_config: None,
67        }
68    }
69}
70
71impl ServerConfig {
72    /// Create a config with API key authentication enabled.
73    pub fn with_api_keys(mut self, keys: impl IntoIterator<Item = String>) -> Self {
74        self.auth = AuthConfig::with_keys(keys);
75        self
76    }
77
78    /// Disable rate limiting.
79    pub fn without_rate_limit(mut self) -> Self {
80        self.rate_limit.max_requests = u32::MAX;
81        self
82    }
83
84    /// Set custom rate limit.
85    pub fn with_rate_limit(mut self, max_requests: u32, window_secs: u64) -> Self {
86        self.rate_limit.max_requests = max_requests;
87        self.rate_limit.window = Duration::from_secs(window_secs);
88        self
89    }
90
91    /// Enable inference from message text.
92    #[cfg(feature = "inference")]
93    pub fn with_inference(mut self) -> Self {
94        self.enable_inference = true;
95        self
96    }
97
98    /// Enable inference with custom configuration.
99    #[cfg(feature = "inference")]
100    pub fn with_inference_config(mut self, config: InferenceConfig) -> Self {
101        self.enable_inference = true;
102        self.inference_config = Some(config);
103        self
104    }
105}