public-ip-tracker/tracker/config/config.go

39 lines
876 B
Go
Raw Normal View History

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
}