public-ip-tracker/tracker/config/config.go
Melora Hugues 3a1bb20a1f
All checks were successful
continuous-integration/drone/push Build is passing
Add monitoring of the current public IP
Ref #4

This commit adds an active monitoring of the current public IP. If a
change is detected, then a message is sent to the given Telegram
channel. Add a few tests for the main monitoring logic.
2023-01-28 14:29:39 +01:00

38 lines
876 B
Go

package config
import (
"encoding/json"
"fmt"
"os"
"time"
)
type TelegramConfig struct {
ChannelID int64 `json:"channel_id"`
Token string `json:"token"`
}
type Config struct {
Telegram *TelegramConfig
PollingFrequency time.Duration
}
type jsonConfig struct {
Telegram *TelegramConfig `json:"telegram"`
PollingFrequency int64 `json:"polling_frequency"`
}
func New(filepath string) (*Config, error) {
content, err := os.ReadFile(filepath)
if err != nil {
return nil, fmt.Errorf("failed to read config file %q: %w", filepath, err)
}
var jsonConf jsonConfig
if err := json.Unmarshal(content, &jsonConf); err != nil {
return nil, fmt.Errorf("failed to parse config file: %w", err)
}
return &Config{
Telegram: jsonConf.Telegram,
PollingFrequency: time.Duration(jsonConf.PollingFrequency) * time.Second,
}, nil
}