m2m/server/
config.rs

1//! Server configuration.
2
3use std::net::SocketAddr;
4use std::time::Duration;
5
6/// Server configuration
7#[derive(Debug, Clone)]
8pub struct ServerConfig {
9    /// Bind address
10    pub addr: SocketAddr,
11    /// Enable security scanning
12    pub security_enabled: bool,
13    /// Security blocking mode
14    pub security_blocking: bool,
15    /// Security block threshold
16    pub block_threshold: f32,
17    /// Session timeout
18    pub session_timeout: Duration,
19    /// Maximum request body size (bytes)
20    pub max_body_size: usize,
21    /// Enable request logging
22    pub logging: bool,
23    /// CORS enabled
24    pub cors_enabled: bool,
25    /// Model path (optional)
26    pub model_path: Option<String>,
27}
28
29impl Default for ServerConfig {
30    fn default() -> Self {
31        Self {
32            addr: "127.0.0.1:3000".parse().unwrap(),
33            security_enabled: true,
34            security_blocking: false,
35            block_threshold: 0.8,
36            session_timeout: Duration::from_secs(300),
37            max_body_size: 10 * 1024 * 1024, // 10MB
38            logging: true,
39            cors_enabled: true,
40            model_path: None,
41        }
42    }
43}
44
45impl ServerConfig {
46    /// Create with custom port
47    pub fn with_port(mut self, port: u16) -> Self {
48        self.addr = format!("127.0.0.1:{port}").parse().unwrap();
49        self
50    }
51
52    /// Bind to all interfaces
53    pub fn bind_all(mut self) -> Self {
54        let port = self.addr.port();
55        self.addr = format!("0.0.0.0:{port}").parse().unwrap();
56        self
57    }
58
59    /// Set address directly
60    pub fn with_addr(mut self, addr: SocketAddr) -> Self {
61        self.addr = addr;
62        self
63    }
64
65    /// Enable security blocking
66    pub fn with_security_blocking(mut self, threshold: f32) -> Self {
67        self.security_blocking = true;
68        self.block_threshold = threshold;
69        self
70    }
71
72    /// Disable security
73    pub fn without_security(mut self) -> Self {
74        self.security_enabled = false;
75        self
76    }
77
78    /// Set model path
79    pub fn with_model(mut self, path: &str) -> Self {
80        self.model_path = Some(path.to_string());
81        self
82    }
83
84    /// Set session timeout
85    pub fn with_session_timeout(mut self, timeout: Duration) -> Self {
86        self.session_timeout = timeout;
87        self
88    }
89
90    /// Set max body size
91    pub fn with_max_body_size(mut self, size: usize) -> Self {
92        self.max_body_size = size;
93        self
94    }
95
96    /// Disable logging
97    pub fn without_logging(mut self) -> Self {
98        self.logging = false;
99        self
100    }
101
102    /// Disable CORS
103    pub fn without_cors(mut self) -> Self {
104        self.cors_enabled = false;
105        self
106    }
107}