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.Use(gin.LoggerWithConfig(gin.LoggerConfig{
|
||||
SkipPaths: []string{
|
||||
"/twitch/public",
|
||||
"/music/public",
|
||||
"/learning/public",
|
||||
},
|
||||
}))
|
||||
srv := gin.New()
|
||||
srv.SetTrustedProxies([]string{"127.0.0.1", "::1"})
|
||||
|
||||
learningService := learning.New(ctx, learning.ServiceConfig{
|
||||
TitleFilePath: "learning-title.txt",
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"crypto/rand"
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
|
|
@ -73,6 +74,8 @@ const (
|
|||
)
|
||||
|
||||
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
|
||||
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(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 {
|
||||
return nil, fmt.Errorf("Failed to open twitch-config.json: %v", err)
|
||||
config := serviceConfig{}
|
||||
if configFile, err := os.OpenFile(CONFIG_PATH, os.O_CREATE | os.O_RDWR, 0600); err != nil {
|
||||
return nil, fmt.Errorf("open %s: %v", CONFIG_FILENAME, err)
|
||||
} else {
|
||||
defer configFile.Close()
|
||||
// TODO: write template file if empty
|
||||
if err := json.NewDecoder(configFile).Decode(&config); err != nil {
|
||||
return nil, fmt.Errorf("Failed to read twitch-config.json: %v", err)
|
||||
|
||||
stat, err := configFile.Stat()
|
||||
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)
|
||||
latestFollowerBroadcast := broadcast.NewBroadcastChannel(
|
||||
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")
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue