tss/app/config.go

77 lines
1.4 KiB
Go
Raw Normal View History

2022-04-27 00:19:05 -04:00
package app
2022-04-26 09:55:59 -04:00
import (
"os"
2022-04-28 19:24:21 -04:00
"path"
2022-04-26 09:55:59 -04:00
"gopkg.in/yaml.v3"
)
2022-04-28 19:24:21 -04:00
type FeedCfg struct {
Name string `yaml:"name"`
Channel string `yaml:"channel"`
Rhash string `yaml:"rhash"`
Url string `yaml:"url"`
Cron string `yaml:"cron"`
Proxy string `yaml:"proxy"`
}
2022-04-26 09:55:59 -04:00
type Config struct {
2022-05-01 23:28:54 -04:00
Proxy string `yaml:"proxy"`
TelegramProxy string `yaml:"telegram_proxy"`
//TODO: read from credential file
TelegramAuthToken string `yaml:"telegram_auth_token"`
DataDir string `yaml:"data_dir"`
LastSuccessPath string `yaml:"last_loaded_path"`
DbDir string `yaml:"db_dir"`
Feeds []FeedCfg `yaml:"feeds"`
2022-04-26 09:55:59 -04:00
}
2022-04-27 00:19:05 -04:00
func ParseConfig(configPath string) (*Config, error) {
2022-04-28 19:24:21 -04:00
2022-04-27 00:19:05 -04:00
cfg, err := os.ReadFile(configPath)
2022-04-26 09:55:59 -04:00
if err != nil {
return nil, err
}
c := Config{}
err = yaml.Unmarshal(cfg, &c)
if err != nil {
return nil, err
}
2022-04-28 19:24:21 -04:00
if c.DataDir == "" {
c.DataDir = "."
}
if c.LastSuccessPath == "" {
c.LastSuccessPath = path.Join(c.DataDir, "last_success.yml")
}
2022-05-01 15:32:41 -04:00
if c.DbDir == "" {
c.DbDir = path.Join(c.DataDir, "feed_data")
}
2022-05-01 19:13:27 -04:00
if c.Proxy != "" {
if c.TelegramProxy == "" {
c.TelegramProxy = c.Proxy
}
for i, _ := range c.Feeds {
feedCfg := &c.Feeds[i]
if feedCfg.Proxy == "" {
feedCfg.Proxy = c.Proxy
}
}
}
for i, _ := range c.Feeds {
feedCfg := &c.Feeds[i]
if feedCfg.Proxy == "NONE" {
feedCfg.Proxy = ""
}
}
2022-04-26 09:55:59 -04:00
return &c, nil
}