This repository has been archived on 2023-09-29. You can view files and clone it, but cannot push or open issues or pull requests.
twitch_data_collector/settings.go
2020-08-19 23:26:26 +02:00

66 lines
1.6 KiB
Go

package main
import (
"bytes"
"io/ioutil"
"time"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2"
"golang.org/x/oauth2/twitch"
"gopkg.in/yaml.v2"
)
var (
settings *Settings
twitchOauthConfig *oauth2.Config
)
type Settings struct {
ClientID string `yaml:"client_id"`
ClientSecret string `yaml:"client_secret"`
MaxFollowers int `yaml:"max_followers"`
MaxSubs int `yaml:"max_subs"`
RedirectURL string `yaml:"redirect_url"`
UpdateInterval time.Duration `yaml:"update_interval"`
VerificationToken string `yaml:"verification_token"`
WebserverPort string `yaml:"webserver_port"`
}
func loadSettings() {
data, err := ioutil.ReadFile(cfg.SettingsFile)
if err != nil {
log.WithError(err).Errorf("Unable to read %s", cfg.SettingsFile)
return
}
s := &Settings{}
b := bytes.NewBuffer(data)
if err := yaml.NewDecoder(b).Decode(s); err != nil {
log.WithError(err).Errorf("Unable to decode %s", cfg.SettingsFile)
return
}
settings = s
twitchOauthConfig = &oauth2.Config{
RedirectURL: settings.RedirectURL,
ClientID: settings.ClientID,
ClientSecret: settings.ClientSecret,
Scopes: []string{"channel:read:subscriptions", "user:read:broadcast", "chat:read", "chat:edit", "channel_read", "channel_editor", "channel_subscriptions", "channel:moderate", "bits:read", "channel:read:redemptions"},
Endpoint: twitch.Endpoint,
}
}
func settingsUpdater() {
loadSettings()
go func() {
c := time.Tick(time.Minute)
for range c {
loadSettings()
}
}()
}