Skip to main content

ferro_rs/config/providers/
server.rs

1use crate::config::env::env;
2
3/// Server configuration
4#[derive(Debug, Clone)]
5pub struct ServerConfig {
6    /// Server host address
7    pub host: String,
8    /// Server port
9    pub port: u16,
10    /// Maximum request body size in bytes (default: 10MB)
11    pub max_body_size: usize,
12}
13
14impl ServerConfig {
15    /// Build config from environment variables
16    pub fn from_env() -> Self {
17        Self {
18            host: env("SERVER_HOST", "127.0.0.1".to_string()),
19            port: env("SERVER_PORT", 8080),
20            max_body_size: env("SERVER_MAX_BODY_SIZE", 10 * 1024 * 1024), // 10MB
21        }
22    }
23
24    /// Create a builder for customizing config
25    pub fn builder() -> ServerConfigBuilder {
26        ServerConfigBuilder::default()
27    }
28}
29
30impl Default for ServerConfig {
31    fn default() -> Self {
32        Self::from_env()
33    }
34}
35
36/// Builder for ServerConfig
37#[derive(Default)]
38pub struct ServerConfigBuilder {
39    host: Option<String>,
40    port: Option<u16>,
41    max_body_size: Option<usize>,
42}
43
44impl ServerConfigBuilder {
45    /// Set the server host
46    pub fn host(mut self, host: impl Into<String>) -> Self {
47        self.host = Some(host.into());
48        self
49    }
50
51    /// Set the server port
52    pub fn port(mut self, port: u16) -> Self {
53        self.port = Some(port);
54        self
55    }
56
57    /// Set the maximum request body size in bytes
58    pub fn max_body_size(mut self, size: usize) -> Self {
59        self.max_body_size = Some(size);
60        self
61    }
62
63    /// Build the ServerConfig
64    pub fn build(self) -> ServerConfig {
65        let default = ServerConfig::from_env();
66        ServerConfig {
67            host: self.host.unwrap_or(default.host),
68            port: self.port.unwrap_or(default.port),
69            max_body_size: self.max_body_size.unwrap_or(default.max_body_size),
70        }
71    }
72}