fix config issue oops

This commit is contained in:
ari melody 2026-07-24 04:08:40 +01:00
parent 626540e728
commit 8e4f353be1
Signed by: ari
GPG key ID: 60B5F0386E3DDB7E
2 changed files with 27 additions and 25 deletions

11
main.go
View file

@ -13,7 +13,6 @@ import (
"codeberg.org/arimelody/ari-stream-tools/learning" "codeberg.org/arimelody/ari-stream-tools/learning"
"codeberg.org/arimelody/ari-stream-tools/music" "codeberg.org/arimelody/ari-stream-tools/music"
"codeberg.org/arimelody/ari-stream-tools/twitch" "codeberg.org/arimelody/ari-stream-tools/twitch"
"codeberg.org/arimelody/ari-stream-tools/utils/dotenv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -25,13 +24,6 @@ func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel() 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) gin.SetMode(gin.ReleaseMode)
host := DEFAULT_HOST host := DEFAULT_HOST
@ -54,9 +46,6 @@ func main() {
musicService := music.New(ctx) musicService := music.New(ctx)
twitchService, err := twitch.New(ctx, twitch.ServiceOptions{ twitchService, err := twitch.New(ctx, twitch.ServiceOptions{
Port: port, 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 { if err != nil {
log.Fatalf("Failed to create Twitch service: %v", err) log.Fatalf("Failed to create Twitch service: %v", err)

View file

@ -40,9 +40,12 @@ type (
ServiceOptions struct { ServiceOptions struct {
Port int16 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 { Service struct {
@ -77,6 +80,20 @@ var publicFS embed.FS
var pagesFS embed.FS var pagesFS embed.FS
func New(ctx context.Context, opts ServiceOptions) (*Service, error) { 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) latestFollowerC := make(chan string)
latestFollowerBroadcast := broadcast.NewBroadcastChannel( latestFollowerBroadcast := broadcast.NewBroadcastChannel(
ctx, latestFollowerC) ctx, latestFollowerC)
@ -105,12 +122,12 @@ func New(ctx context.Context, opts ServiceOptions) (*Service, error) {
Broadcast: latestCheerBroadcast, Broadcast: latestCheerBroadcast,
}, },
}, },
channelName: opts.ChannelName, channelName: config.ChannelName,
clientID: opts.ClientID, clientID: config.ClientID,
clientSecret: opts.ClientSecret, clientSecret: config.ClientSecret,
oauthConfig: &oauth2.Config{ oauthConfig: &oauth2.Config{
ClientID: opts.ClientID, ClientID: config.ClientID,
ClientSecret: opts.ClientSecret, ClientSecret: config.ClientSecret,
Endpoint: oauth2.Endpoint{ Endpoint: oauth2.Endpoint{
AuthURL: "https://id.twitch.tv/oauth2/authorize", AuthURL: "https://id.twitch.tv/oauth2/authorize",
TokenURL: "https://id.twitch.tv/oauth2/token", 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 authFile, err := os.OpenFile(path.Join(DATA_PATH, "twitch-auth.json"), os.O_RDONLY, 0600); err != nil {
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 !os.IsNotExist(err) { if !os.IsNotExist(err) {
log.Fatalf("Failed to open twitch-auth.json: %v", err) log.Fatalf("Failed to open twitch-auth.json: %v", err)
} }
} else { } else {
defer authFile.Close()
srv.oauthToken = &oauth2.Token{} srv.oauthToken = &oauth2.Token{}
err = json.NewDecoder(authFile).Decode(srv.oauthToken) err = json.NewDecoder(authFile).Decode(srv.oauthToken)
if err != nil { if err != nil {
log.Printf("Failed to read twitch-auth.json: %v", err) 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 { if data, err := os.ReadFile(path.Join(DATA_PATH, "state", LABEL_LATEST_FOLLOWER)); err == nil {
srv.labels.LatestFollower.Text = string(data) srv.labels.LatestFollower.Text = string(data)