Compare commits
No commits in common. "accf60ed74391b4ed556f357f73f234862f2408b" and "8e4f353be175d6fc85a985287465f7b99fc5cb84" have entirely different histories.
accf60ed74
...
8e4f353be1
3 changed files with 29 additions and 50 deletions
35
main.go
35
main.go
|
|
@ -40,30 +40,31 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
srv := gin.New()
|
|
||||||
srv.SetTrustedProxies([]string{"127.0.0.1", "::1"})
|
|
||||||
|
|
||||||
learningService := learning.New(ctx, learning.ServiceConfig{
|
learningService := learning.New(ctx, learning.ServiceConfig{
|
||||||
TitleFilePath: "learning-title.txt",
|
TitleFilePath: "learning-title.txt",
|
||||||
})
|
})
|
||||||
|
musicService := music.New(ctx)
|
||||||
|
twitchService, err := twitch.New(ctx, twitch.ServiceOptions{
|
||||||
|
Port: port,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to create Twitch service: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
srv := gin.Default()
|
||||||
|
srv.Use(gin.LoggerWithConfig(gin.LoggerConfig{
|
||||||
|
SkipPaths: []string{
|
||||||
|
"/twitch/public",
|
||||||
|
"/music/public",
|
||||||
|
"/learning/public",
|
||||||
|
},
|
||||||
|
}))
|
||||||
learningService.BindRoutes(srv.Group("/learning"))
|
learningService.BindRoutes(srv.Group("/learning"))
|
||||||
|
|
||||||
musicService, err := music.New(ctx)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to create music service: %v", err)
|
|
||||||
} else {
|
|
||||||
musicService.BindRoutes(srv.Group("/music"))
|
musicService.BindRoutes(srv.Group("/music"))
|
||||||
go musicService.Run(ctx)
|
|
||||||
}
|
|
||||||
|
|
||||||
twitchService, err := twitch.New(ctx, twitch.ServiceOptions{ Port: port })
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to create Twitch service: %v", err)
|
|
||||||
} else {
|
|
||||||
twitchService.BindRoutes(srv.Group("/twitch"))
|
twitchService.BindRoutes(srv.Group("/twitch"))
|
||||||
go twitchService.Run(ctx)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
go musicService.Run(ctx)
|
||||||
|
go twitchService.Run(ctx)
|
||||||
go func() {
|
go func() {
|
||||||
log.Printf("Now serving at http://%s:%d\n", host, port)
|
log.Printf("Now serving at http://%s:%d\n", host, port)
|
||||||
failed <- srv.Run(fmt.Sprintf("%s:%d", host, port))
|
failed <- srv.Run(fmt.Sprintf("%s:%d", host, port))
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -51,11 +50,7 @@ type (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func New(ctx context.Context) (*Service, error) {
|
func New(ctx context.Context) *Service {
|
||||||
if runtime.GOOS != "linux" {
|
|
||||||
return nil, fmt.Errorf("Platform %s is unsupported", runtime.GOOS)
|
|
||||||
}
|
|
||||||
|
|
||||||
nextTrack := make(chan *trackMetadata)
|
nextTrack := make(chan *trackMetadata)
|
||||||
nextPlaying := make(chan bool)
|
nextPlaying := make(chan bool)
|
||||||
|
|
||||||
|
|
@ -68,7 +63,7 @@ func New(ctx context.Context) (*Service, error) {
|
||||||
playingBroadcast: broadcast.NewBroadcastChannel(ctx, nextPlaying),
|
playingBroadcast: broadcast.NewBroadcastChannel(ctx, nextPlaying),
|
||||||
}
|
}
|
||||||
|
|
||||||
return srv, nil
|
return srv
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *Service) BindRoutes(group *gin.RouterGroup) {
|
func (srv *Service) BindRoutes(group *gin.RouterGroup) {
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"embed"
|
"embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
|
@ -74,8 +73,6 @@ 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
|
||||||
|
|
@ -86,31 +83,17 @@ 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) }
|
||||||
|
|
||||||
config := serviceConfig{}
|
var config serviceConfig
|
||||||
if configFile, err := os.OpenFile(CONFIG_PATH, os.O_CREATE | os.O_RDWR, 0600); err != nil {
|
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("open %s: %v", CONFIG_FILENAME, err)
|
log.Fatalf("Failed to open twitch-config.json: %v", err)
|
||||||
} else {
|
} else {
|
||||||
defer configFile.Close()
|
defer configFile.Close()
|
||||||
|
// TODO: write template file if empty
|
||||||
stat, err := configFile.Stat()
|
if err := json.NewDecoder(configFile).Decode(&config); err != nil {
|
||||||
if err != nil { return nil, fmt.Errorf("stat %s: %v", CONFIG_FILENAME, err) }
|
log.Fatalf("Failed to read twitch-config.json: %v", 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)
|
||||||
|
|
@ -271,7 +254,7 @@ func (srv *Service) BindRoutes(group *gin.RouterGroup) {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
group.GET("/label", func(ctx *gin.Context) {
|
group.GET("/labels", 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