use serde::{Deserialize, Serialize};
use std::net::SocketAddr;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ServerConfig {
#[serde(default = "default_host")]
pub host: String,
#[serde(default = "default_port")]
pub port: u16,
#[serde(default)]
pub cors_origins: Vec<String>,
#[serde(default = "default_max_connections")]
pub max_connections: usize,
}
fn default_host() -> String {
"0.0.0.0".to_string()
}
fn default_port() -> u16 {
3000
}
fn default_max_connections() -> usize {
1000
}
impl Default for ServerConfig {
fn default() -> Self {
Self {
host: default_host(),
port: default_port(),
cors_origins: vec![],
max_connections: default_max_connections(),
}
}
}
impl ServerConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_host(mut self, host: impl Into<String>) -> Self {
self.host = host.into();
self
}
pub fn with_port(mut self, port: u16) -> Self {
self.port = port;
self
}
pub fn with_cors_origin(mut self, origin: impl Into<String>) -> Self {
self.cors_origins.push(origin.into());
self
}
pub fn socket_addr(&self) -> SocketAddr {
format!("{}:{}", self.host, self.port)
.parse()
.expect("Invalid socket address")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_config_default() {
let config = ServerConfig::default();
assert_eq!(config.host, "0.0.0.0");
assert_eq!(config.port, 3000);
}
#[test]
fn test_config_builder() {
let config = ServerConfig::new()
.with_host("127.0.0.1")
.with_port(8080)
.with_cors_origin("http://localhost:3000");
assert_eq!(config.host, "127.0.0.1");
assert_eq!(config.port, 8080);
assert_eq!(config.cors_origins.len(), 1);
}
#[test]
fn test_socket_addr() {
let config = ServerConfig::new().with_host("127.0.0.1").with_port(8080);
let addr = config.socket_addr();
assert_eq!(addr.to_string(), "127.0.0.1:8080");
}
}