use super::types::GatewayConfigBuilder;
use crate::config::{
AuthConfig, Config, GatewayConfig, ProviderConfig, ServerConfig, StorageConfig,
};
use crate::utils::data::type_utils::Builder;
use crate::utils::error::gateway_error::{GatewayError, Result};
use std::collections::HashMap;
impl GatewayConfigBuilder {
pub fn new() -> Self {
Self {
server: None,
auth: None,
storage: None,
providers: Vec::new(),
features: HashMap::new(),
}
}
pub fn with_server(mut self, config: ServerConfig) -> Self {
self.server = Some(config);
self
}
pub fn with_auth(mut self, config: AuthConfig) -> Self {
self.auth = Some(config);
self
}
pub fn with_storage(mut self, config: StorageConfig) -> Self {
self.storage = Some(config);
self
}
pub fn add_provider(mut self, config: ProviderConfig) -> Self {
self.providers.push(config);
self
}
pub fn add_providers(mut self, configs: Vec<ProviderConfig>) -> Self {
self.providers.extend(configs);
self
}
pub fn enable_feature(mut self, feature: impl Into<String>) -> Self {
self.features.insert(feature.into(), true);
self
}
pub fn disable_feature(mut self, feature: impl Into<String>) -> Self {
self.features.insert(feature.into(), false);
self
}
pub fn build(self) -> Result<Config> {
let gateway = GatewayConfig {
schema_version: "1.0".to_string(),
server: self.server.unwrap_or_default(),
auth: self.auth.unwrap_or_default(),
storage: self.storage.unwrap_or_default(),
providers: self.providers,
router: crate::config::models::router::GatewayRouterConfig::default(),
monitoring: crate::config::models::monitoring::MonitoringConfig::default(),
cache: crate::config::models::cache::CacheConfig::default(),
rate_limit: crate::config::models::rate_limit::RateLimitConfig::default(),
enterprise: crate::config::models::enterprise::EnterpriseConfig::default(),
pricing: crate::config::models::gateway::GatewayPricingConfig::default(),
};
let config = Config { gateway };
if let Err(e) = config.gateway.validate() {
return Err(GatewayError::Config(e));
}
Ok(config)
}
pub fn build_or_panic(self) -> Config {
self.build().unwrap_or_else(|e| {
panic!("Failed to build configuration: {}", e);
})
}
pub fn build_or_default(self) -> Config {
self.build().unwrap_or_else(|e| {
tracing::warn!("Configuration validation failed: {}, using defaults", e);
Config::default()
})
}
}
impl Default for GatewayConfigBuilder {
fn default() -> Self {
Self::new()
}
}
impl Builder<Config> for GatewayConfigBuilder {
fn build(self) -> Config {
self.build_or_default()
}
}