use crate::Error;
use figment::providers::Format;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebhookConfig {
pub endpoint: String,
pub auth_token: Option<String>,
pub custom_headers: Option<HashMap<String, String>>,
pub max_retries: u32,
pub timeout: u64,
}
impl WebhookConfig {
pub fn validate(&self) -> Result<(), String> {
if self.endpoint.trim().is_empty() {
return Err("Webhook endpoint cannot be empty".to_string());
}
if self.timeout == 0 {
return Err("Webhook timeout must be greater than 0".to_string());
}
if self.max_retries > 10 {
return Err("Maximum retry count cannot exceed 10".to_string());
}
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KafkaConfig {
pub brokers: String,
pub topic: String,
pub max_retries: u32,
pub timeout: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MqttConfig {
pub broker: String,
pub port: u16,
pub client_id: String,
pub topic: String,
pub max_retries: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum AdapterConfig {
Webhook(WebhookConfig),
Kafka(KafkaConfig),
Mqtt(MqttConfig),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HttpProducerConfig {
#[serde(default = "default_http_port")]
pub port: u16,
}
impl Default for HttpProducerConfig {
fn default() -> Self {
Self {
port: default_http_port(),
}
}
}
fn default_http_port() -> u16 {
3000
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NotificationConfig {
#[serde(default = "default_store_path")]
pub store_path: String,
#[serde(default = "default_channel_capacity")]
pub channel_capacity: usize,
pub adapters: Vec<AdapterConfig>,
#[serde(default)]
pub http: HttpProducerConfig,
}
impl Default for NotificationConfig {
fn default() -> Self {
Self {
store_path: default_store_path(),
channel_capacity: default_channel_capacity(),
adapters: Vec::new(),
http: HttpProducerConfig::default(),
}
}
}
impl NotificationConfig {
pub fn new() -> Self {
Self::default()
}
pub fn from_file(path: &str) -> Result<Self, Error> {
let config = figment::Figment::new()
.merge(figment::providers::Toml::file(path))
.extract()?;
Ok(config)
}
pub fn load() -> Result<Self, Error> {
let figment = figment::Figment::new()
.merge(figment::providers::Toml::file("event.toml"))
.merge(figment::providers::Yaml::file("event.yaml"))
.merge(figment::providers::Env::prefixed("EVENT_NOTIF_"));
Ok(figment.extract()?)
}
pub fn from_env_file(path: &str) -> Result<Self, Error> {
dotenv::from_path(path)
.map_err(|e| Error::ConfigError(format!("unable to load env file: {}", e)))?;
let figment =
figment::Figment::new().merge(figment::providers::Env::prefixed("EVENT_NOTIF_"));
Ok(figment.extract()?)
}
}
fn default_store_path() -> String {
std::env::temp_dir()
.join("event-notification")
.to_string_lossy()
.to_string()
}
fn default_channel_capacity() -> usize {
10000 }