2023-01-25 19:42:18 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-01-28 13:29:39 +00:00
|
|
|
"time"
|
2023-01-25 19:42:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type TelegramConfig struct {
|
|
|
|
ChannelID int64 `json:"channel_id"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
2023-01-28 13:29:39 +00:00
|
|
|
Telegram *TelegramConfig
|
|
|
|
PollingFrequency time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonConfig struct {
|
|
|
|
Telegram *TelegramConfig `json:"telegram"`
|
|
|
|
PollingFrequency int64 `json:"polling_frequency"`
|
2023-01-25 19:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2023-01-28 13:29:39 +00:00
|
|
|
var jsonConf jsonConfig
|
|
|
|
if err := json.Unmarshal(content, &jsonConf); err != nil {
|
2023-01-25 19:42:18 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse config file: %w", err)
|
|
|
|
}
|
2023-01-28 13:29:39 +00:00
|
|
|
return &Config{
|
|
|
|
Telegram: jsonConf.Telegram,
|
|
|
|
PollingFrequency: time.Duration(jsonConf.PollingFrequency) * time.Second,
|
|
|
|
}, nil
|
2023-01-25 19:42:18 +00:00
|
|
|
}
|