improve fault tolerance and OS compat beyond linux
This commit is contained in:
parent
8e4f353be1
commit
75bd36a332
3 changed files with 30 additions and 20 deletions
33
main.go
33
main.go
|
|
@ -40,17 +40,6 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
learningService := learning.New(ctx, learning.ServiceConfig{
|
|
||||||
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 := gin.Default()
|
||||||
srv.Use(gin.LoggerWithConfig(gin.LoggerConfig{
|
srv.Use(gin.LoggerWithConfig(gin.LoggerConfig{
|
||||||
SkipPaths: []string{
|
SkipPaths: []string{
|
||||||
|
|
@ -59,12 +48,28 @@ func main() {
|
||||||
"/learning/public",
|
"/learning/public",
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
learningService.BindRoutes(srv.Group("/learning"))
|
|
||||||
musicService.BindRoutes(srv.Group("/music"))
|
|
||||||
twitchService.BindRoutes(srv.Group("/twitch"))
|
|
||||||
|
|
||||||
|
learningService := learning.New(ctx, learning.ServiceConfig{
|
||||||
|
TitleFilePath: "learning-title.txt",
|
||||||
|
})
|
||||||
|
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"))
|
||||||
go musicService.Run(ctx)
|
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"))
|
||||||
go twitchService.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,6 +7,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -50,7 +51,11 @@ type (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func New(ctx context.Context) *Service {
|
func New(ctx context.Context) (*Service, error) {
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
@ -63,7 +68,7 @@ func New(ctx context.Context) *Service {
|
||||||
playingBroadcast: broadcast.NewBroadcastChannel(ctx, nextPlaying),
|
playingBroadcast: broadcast.NewBroadcastChannel(ctx, nextPlaying),
|
||||||
}
|
}
|
||||||
|
|
||||||
return srv
|
return srv, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *Service) BindRoutes(group *gin.RouterGroup) {
|
func (srv *Service) BindRoutes(group *gin.RouterGroup) {
|
||||||
|
|
|
||||||
|
|
@ -85,12 +85,12 @@ func New(ctx context.Context, opts ServiceOptions) (*Service, error) {
|
||||||
|
|
||||||
var config serviceConfig
|
var 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(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)
|
return nil, fmt.Errorf("Failed to open twitch-config.json: %v", err)
|
||||||
} else {
|
} else {
|
||||||
defer configFile.Close()
|
defer configFile.Close()
|
||||||
// TODO: write template file if empty
|
// TODO: write template file if empty
|
||||||
if err := json.NewDecoder(configFile).Decode(&config); err != nil {
|
if err := json.NewDecoder(configFile).Decode(&config); err != nil {
|
||||||
log.Fatalf("Failed to read twitch-config.json: %v", err)
|
return nil, fmt.Errorf("Failed to read twitch-config.json: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue