use polaris_system::resource::GlobalResource;
use std::net::SocketAddr;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AppConfig {
host: String,
port: u16,
cors_origins: Vec<String>,
}
impl GlobalResource for AppConfig {}
impl Default for AppConfig {
fn default() -> Self {
Self {
host: "127.0.0.1".to_string(),
port: 3000,
cors_origins: Vec::new(),
}
}
}
impl AppConfig {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_host(mut self, host: impl Into<String>) -> Self {
self.host = host.into();
self
}
#[must_use]
pub fn with_port(mut self, port: u16) -> Self {
self.port = port;
self
}
#[must_use]
pub fn with_cors_origin(mut self, origin: impl Into<String>) -> Self {
self.cors_origins.push(origin.into());
self
}
#[must_use]
pub fn host(&self) -> &str {
&self.host
}
#[must_use]
pub fn port(&self) -> u16 {
self.port
}
#[must_use]
pub fn cors_origins(&self) -> &[String] {
&self.cors_origins
}
#[must_use]
pub fn addr(&self) -> SocketAddr {
let raw = format!("{}:{}", self.host, self.port);
raw.parse()
.expect("AppConfig host must be a valid IP address")
}
}