Skip to content

How to write a configuration file for .env with the above structure NotiferConfig? #658

@houseme

Description

@houseme

rust code:

#[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,
}

// Configuration for the Kafka adapter.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KafkaConfig {
    pub brokers: String,
    pub topic: String,
    pub max_retries: u32,
    pub timeout: u64,
}

/// Configuration for the MQTT adapter.
#[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,
}

/// Configuration for the notification system.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum AdapterConfig {
    Webhook(WebhookConfig),
    Kafka(KafkaConfig),
    Mqtt(MqttConfig),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NotifierConfig {
    #[serde(default = "default_store_path")]
    pub store_path: String,
    #[serde(default = "default_channel_capacity")]
    pub channel_capacity: usize,
    pub adapters: Vec<AdapterConfig>,
}

toml file:

# config.toml
store_path = "/var/log/event-notifier"
channel_capacity = 5000

[[adapters]]
type = "Webhook"
endpoint = "https://api.example.com/webhook/toml"
auth_token = "your-auth-token"
max_retries = 3
timeout = 50

[adapters.0.custom_headers]
X-Custom = "value"

[[adapters]]
type = "Kafka"
brokers = "localhost:9092"
topic = "notifications"
max_retries = 3
timeout = 60

[[adapters]]
type = "Mqtt"
broker = "mqtt.example.com"
port = 1883
client_id = "event-notifier"
topic = "events"
max_retries = 3

env file:

# Global config
NOTIFIER_STORE_PATH=/var/log/event-notification
NOTIFIER_CHANNEL_CAPACITY=5000

# Adapters (array)
NOTIFIER_ADAPTERS_0_TYPE=Webhook
NOTIFIER_ADAPTERS_0_ENDPOINT=https://api.example.com/webhook
NOTIFIER_ADAPTERS_0_AUTH_TOKEN=your-token
NOTIFIER_ADAPTERS_0_MAX_RETRIES=3
NOTIFIER_ADAPTERS_0_TIMEOUT=50

NOTIFIER_ADAPTERS_1_TYPE=Kafka
NOTIFIER_ADAPTERS_1_BROKERS=localhost:9092
NOTIFIER_ADAPTERS_1_TOPIC=notifications
NOTIFIER_ADAPTERS_1_MAX_RETRIES=3
NOTIFIER_ADAPTERS_1_TIMEOUT=60

NOTIFIER_ADAPTERS_2_TYPE=Mqtt
NOTIFIER_ADAPTERS_2_BROKER=mqtt.example.com
NOTIFIER_ADAPTERS_2_PORT=1883
NOTIFIER_ADAPTERS_2_CLIENT_ID=event-notifier
NOTIFIER_ADAPTERS_2_TOPIC=events
NOTIFIER_ADAPTERS_2_MAX_RETRIES=3

read .env file error:

Failed to deserialize config: invalid type: map, expected a sequence for key `adapters`
# Global config
NOTIFIER_STORE_PATH=/var/log/event-notification
NOTIFIER_CHANNEL_CAPACITY=5000

# Adapters (array)
NOTIFIER_ADAPTERS_0__TYPE=Webhook
NOTIFIER_ADAPTERS_0__ENDPOINT=https://api.example.com/webhook
NOTIFIER_ADAPTERS_0__AUTH_TOKEN=your-token
NOTIFIER_ADAPTERS_0__MAX_RETRIES=3
NOTIFIER_ADAPTERS_0__TIMEOUT=50

NOTIFIER_ADAPTERS_1__TYPE=Kafka
NOTIFIER_ADAPTERS_1__BROKERS=localhost:9092
NOTIFIER_ADAPTERS_1__TOPIC=notifications
NOTIFIER_ADAPTERS_1__MAX_RETRIES=3
NOTIFIER_ADAPTERS_1__TIMEOUT=60

NOTIFIER_ADAPTERS_2__TYPE=Mqtt
NOTIFIER_ADAPTERS_2__BROKER=mqtt.example.com
NOTIFIER_ADAPTERS_2__PORT=1883
NOTIFIER_ADAPTERS_2__CLIENT_ID=event-notifier
NOTIFIER_ADAPTERS_2__TOPIC=events
NOTIFIER_ADAPTERS_2__MAX_RETRIES=3

read .env file error:

Failed to deserialize config: missing field `adapters`

#631 #536 @epage

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions