Config Module π
config.go π οΈ
Purpose
The config.go file defines the application's configuration and provides utilities to load it from a YAML file. This module is essential for managing dynamic settings across various components.
Key Components π
1. Message Struct
- Purpose: Represents notification messages used across the system.
- Key Features:
- Implements
sql.Scannerto parse JSON from the database into aMessagestruct. - Implements
driver.Valuerto convert theMessagestruct into JSON for database storage. - Example:
2. ScheduledJob Struct
- Purpose: Defines the fields required for scheduling notification jobs.
- Key Fields:
ID: Unique identifier for the job.Name: Name of the job.NotificationType: Type of notification (e.g., email, Slack).Recipient: Target recipient.Message: Message content inMessagestruct format.ScheduleExpression: Cron expression for job timing.- Example:
3. Notifier Interface
- Purpose: Defines the methods that custom notifiers must implement.
- Methods:
Name() string: Returns the name of the notifier.Type() string: Returns the type of the notifier.Notify(message *Message) error: Sends a notification based on the provided message.- Example Implementation:
4. LoadConfig Function
- Purpose: Reads and parses the configuration file into a structured
Configobject. - Steps:
- Opens the
config.yamlfile. - Parses its content into a
Configstruct. - Returns the structured configuration for use across the application.
- Example:
Config Struct π¦
Purpose
Encapsulates the entire configuration for the application, including database settings, notification channels, and scheduler options.
Key Fields
- Database Config:
Host,Port,User,Password,Name- Channels Config:
- Email, Slack, SMS, Webhook configurations
- Scheduler Flag:
- Enables or disables the job scheduler.
Example
type Config struct {
Database DatabaseConfig `yaml:"database"`
Channels map[string]ChannelConfig `yaml:"channels"`
Scheduler bool `yaml:"scheduler"`
}
type DatabaseConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
User string `yaml:"user"`
Password string `yaml:"password"`
Name string `yaml:"name"`
}
type ChannelConfig struct {
Enabled bool `yaml:"enabled"`
Host string `yaml:"host"`
Port int `yaml:"port"`
Username string `yaml:"username"`
Password string `yaml:"password"`
WebhookURL string `yaml:"webhook_url"`
}
Example Flow π
- Load Configuration:
- The application reads
config.yamlon startup. - Parses the YAML file into a structured
Configobject. - Use Configuration:
- Components access configuration values directly from the
Configobject. - Example:
go dbConnStr := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", cfg.Database.User, cfg.Database.Password, cfg.Database.Host, cfg.Database.Port, cfg.Database.Name, )
This documentation provides a detailed explanation of config.go, highlighting its essential role in managing the applicationβs dynamic settings. π οΈ Happy configuring!