improve handling of empty/incomplete twitch config
This commit is contained in:
parent
75bd36a332
commit
accf60ed74
2 changed files with 26 additions and 15 deletions
10
main.go
10
main.go
|
|
@ -40,14 +40,8 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
srv := gin.Default()
|
srv := gin.New()
|
||||||
srv.Use(gin.LoggerWithConfig(gin.LoggerConfig{
|
srv.SetTrustedProxies([]string{"127.0.0.1", "::1"})
|
||||||
SkipPaths: []string{
|
|
||||||
"/twitch/public",
|
|
||||||
"/music/public",
|
|
||||||
"/learning/public",
|
|
||||||
},
|
|
||||||
}))
|
|
||||||
|
|
||||||
learningService := learning.New(ctx, learning.ServiceConfig{
|
learningService := learning.New(ctx, learning.ServiceConfig{
|
||||||
TitleFilePath: "learning-title.txt",
|
TitleFilePath: "learning-title.txt",
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"embed"
|
"embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
|
@ -73,6 +74,8 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
var DATA_PATH string = path.Join(config.CONFIG_DIR, "twitch")
|
var DATA_PATH string = path.Join(config.CONFIG_DIR, "twitch")
|
||||||
|
var CONFIG_FILENAME string = "twitch-config.json"
|
||||||
|
var CONFIG_PATH string = path.Join(DATA_PATH, CONFIG_FILENAME)
|
||||||
|
|
||||||
//go:embed public
|
//go:embed public
|
||||||
var publicFS embed.FS
|
var publicFS embed.FS
|
||||||
|
|
@ -83,17 +86,31 @@ func New(ctx context.Context, opts ServiceOptions) (*Service, error) {
|
||||||
if err := os.MkdirAll(DATA_PATH, 0750); err != nil { panic(err) }
|
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) }
|
if err := os.MkdirAll(path.Join(DATA_PATH, "state"), 0750); err != nil { panic(err) }
|
||||||
|
|
||||||
var config serviceConfig
|
config := serviceConfig{}
|
||||||
if configFile, err := os.OpenFile(path.Join(DATA_PATH, "twitch-config.json"), os.O_CREATE | os.O_RDWR, 0600); err != nil {
|
if configFile, err := os.OpenFile(CONFIG_PATH, os.O_CREATE | os.O_RDWR, 0600); err != nil {
|
||||||
return nil, fmt.Errorf("Failed to open twitch-config.json: %v", err)
|
return nil, fmt.Errorf("open %s: %v", CONFIG_FILENAME, err)
|
||||||
} else {
|
} else {
|
||||||
defer configFile.Close()
|
defer configFile.Close()
|
||||||
// TODO: write template file if empty
|
|
||||||
if err := json.NewDecoder(configFile).Decode(&config); err != nil {
|
stat, err := configFile.Stat()
|
||||||
return nil, fmt.Errorf("Failed to read twitch-config.json: %v", err)
|
if err != nil { return nil, fmt.Errorf("stat %s: %v", CONFIG_FILENAME, err) }
|
||||||
|
|
||||||
|
if stat.Size() == 0 {
|
||||||
|
enc := json.NewEncoder(configFile)
|
||||||
|
enc.SetIndent("", "\t")
|
||||||
|
if err := enc.Encode(&config); err != nil {
|
||||||
|
return nil, fmt.Errorf("write %s: %v", CONFIG_FILENAME, err)
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("Config file is empty: %s", CONFIG_PATH)
|
||||||
|
} else if err := json.NewDecoder(configFile).Decode(&config); err != nil {
|
||||||
|
return nil, fmt.Errorf("decode %s: %v", CONFIG_FILENAME, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(config.ChannelName) == 0 { return nil, errors.New("config: channel_name cannot be empty") }
|
||||||
|
if len(config.ClientID) == 0 { return nil, errors.New("config: client_id cannot be empty") }
|
||||||
|
if len(config.ClientSecret) == 0 { return nil, errors.New("config: client_secret cannot be empty") }
|
||||||
|
|
||||||
latestFollowerC := make(chan string)
|
latestFollowerC := make(chan string)
|
||||||
latestFollowerBroadcast := broadcast.NewBroadcastChannel(
|
latestFollowerBroadcast := broadcast.NewBroadcastChannel(
|
||||||
ctx, latestFollowerC)
|
ctx, latestFollowerC)
|
||||||
|
|
@ -254,7 +271,7 @@ func (srv *Service) BindRoutes(group *gin.RouterGroup) {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
group.GET("/labels", func(ctx *gin.Context) {
|
group.GET("/label", func(ctx *gin.Context) {
|
||||||
http.ServeFileFS(ctx.Writer, ctx.Request, pagesFS, "pages/labels.html")
|
http.ServeFileFS(ctx.Writer, ctx.Request, pagesFS, "pages/labels.html")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue