ferro_rs/config/providers/
server.rs1use crate::config::env::env;
2
3#[derive(Debug, Clone)]
5pub struct ServerConfig {
6 pub host: String,
8 pub port: u16,
10 pub max_body_size: usize,
12}
13
14impl ServerConfig {
15 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), }
22 }
23
24 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#[derive(Default)]
38pub struct ServerConfigBuilder {
39 host: Option<String>,
40 port: Option<u16>,
41 max_body_size: Option<usize>,
42}
43
44impl ServerConfigBuilder {
45 pub fn host(mut self, host: impl Into<String>) -> Self {
47 self.host = Some(host.into());
48 self
49 }
50
51 pub fn port(mut self, port: u16) -> Self {
53 self.port = Some(port);
54 self
55 }
56
57 pub fn max_body_size(mut self, size: usize) -> Self {
59 self.max_body_size = Some(size);
60 self
61 }
62
63 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}