diff --git a/main.go b/main.go index 4a0542b..257bcb1 100644 --- a/main.go +++ b/main.go @@ -13,7 +13,6 @@ import ( "codeberg.org/arimelody/ari-stream-tools/learning" "codeberg.org/arimelody/ari-stream-tools/music" "codeberg.org/arimelody/ari-stream-tools/twitch" - "codeberg.org/arimelody/ari-stream-tools/utils/dotenv" "github.com/gin-gonic/gin" ) @@ -25,13 +24,6 @@ func main() { ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer cancel() - envars, err := dotenv.LoadEnv() - if err != nil { - if !os.IsNotExist(err) { - log.Fatalf("Failed to read .env: %v", err) - } - } - gin.SetMode(gin.ReleaseMode) host := DEFAULT_HOST @@ -54,9 +46,6 @@ func main() { musicService := music.New(ctx) twitchService, err := twitch.New(ctx, twitch.ServiceOptions{ Port: port, - ChannelName: dotenv.SafeGet(envars, "TWITCH_CHANNEL_NAME"), - ClientID: dotenv.SafeGet(envars, "TWITCH_CLIENT_ID"), - ClientSecret: dotenv.SafeGet(envars, "TWITCH_CLIENT_SECRET"), }) if err != nil { log.Fatalf("Failed to create Twitch service: %v", err) diff --git a/twitch/twitch.go b/twitch/twitch.go index 527bc19..6bbb478 100644 --- a/twitch/twitch.go +++ b/twitch/twitch.go @@ -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)