fix config issue oops
This commit is contained in:
parent
626540e728
commit
8e4f353be1
2 changed files with 27 additions and 25 deletions
|
|
@ -40,9 +40,12 @@ type (
|
|||
|
||||
ServiceOptions struct {
|
||||
Port int16
|
||||
ChannelName string
|
||||
ClientID string
|
||||
ClientSecret string
|
||||
}
|
||||
|
||||
serviceConfig struct {
|
||||
ChannelName string `json:"channel_name"`
|
||||
ClientID string `json:"client_id"`
|
||||
ClientSecret string `json:"client_secret"`
|
||||
}
|
||||
|
||||
Service struct {
|
||||
|
|
@ -77,6 +80,20 @@ var publicFS embed.FS
|
|||
var pagesFS embed.FS
|
||||
|
||||
func New(ctx context.Context, opts ServiceOptions) (*Service, error) {
|
||||
if err := os.MkdirAll(DATA_PATH, 0750); err != nil { panic(err) }
|
||||
if err := os.MkdirAll(path.Join(DATA_PATH, "state"), 0750); err != nil { panic(err) }
|
||||
|
||||
var config serviceConfig
|
||||
if configFile, err := os.OpenFile(path.Join(DATA_PATH, "twitch-config.json"), os.O_CREATE | os.O_RDWR, 0600); err != nil {
|
||||
log.Fatalf("Failed to open twitch-config.json: %v", err)
|
||||
} else {
|
||||
defer configFile.Close()
|
||||
// TODO: write template file if empty
|
||||
if err := json.NewDecoder(configFile).Decode(&config); err != nil {
|
||||
log.Fatalf("Failed to read twitch-config.json: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
latestFollowerC := make(chan string)
|
||||
latestFollowerBroadcast := broadcast.NewBroadcastChannel(
|
||||
ctx, latestFollowerC)
|
||||
|
|
@ -105,12 +122,12 @@ func New(ctx context.Context, opts ServiceOptions) (*Service, error) {
|
|||
Broadcast: latestCheerBroadcast,
|
||||
},
|
||||
},
|
||||
channelName: opts.ChannelName,
|
||||
clientID: opts.ClientID,
|
||||
clientSecret: opts.ClientSecret,
|
||||
channelName: config.ChannelName,
|
||||
clientID: config.ClientID,
|
||||
clientSecret: config.ClientSecret,
|
||||
oauthConfig: &oauth2.Config{
|
||||
ClientID: opts.ClientID,
|
||||
ClientSecret: opts.ClientSecret,
|
||||
ClientID: config.ClientID,
|
||||
ClientSecret: config.ClientSecret,
|
||||
Endpoint: oauth2.Endpoint{
|
||||
AuthURL: "https://id.twitch.tv/oauth2/authorize",
|
||||
TokenURL: "https://id.twitch.tv/oauth2/token",
|
||||
|
|
@ -132,22 +149,18 @@ func New(ctx context.Context, opts ServiceOptions) (*Service, error) {
|
|||
},
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(DATA_PATH, 0750); err != nil { panic(err) }
|
||||
if err := os.MkdirAll(path.Join(DATA_PATH, "state"), 0750); err != nil { panic(err) }
|
||||
|
||||
authFile, err := os.OpenFile(path.Join(DATA_PATH, "twitch-auth.json"), os.O_RDONLY, 0600)
|
||||
if err != nil {
|
||||
if authFile, err := os.OpenFile(path.Join(DATA_PATH, "twitch-auth.json"), os.O_RDONLY, 0600); err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
log.Fatalf("Failed to open twitch-auth.json: %v", err)
|
||||
}
|
||||
} else {
|
||||
defer authFile.Close()
|
||||
srv.oauthToken = &oauth2.Token{}
|
||||
err = json.NewDecoder(authFile).Decode(srv.oauthToken)
|
||||
if err != nil {
|
||||
log.Printf("Failed to read twitch-auth.json: %v", err)
|
||||
}
|
||||
}
|
||||
authFile.Close()
|
||||
|
||||
if data, err := os.ReadFile(path.Join(DATA_PATH, "state", LABEL_LATEST_FOLLOWER)); err == nil {
|
||||
srv.labels.LatestFollower.Text = string(data)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue