overhaul config, refactor twitch labels, init satellite

This commit is contained in:
ari melody 2026-07-24 17:24:07 +01:00
parent b2485eda79
commit 097a36ac69
Signed by: ari
GPG key ID: CF99829C92678188
6 changed files with 161 additions and 152 deletions

View file

@ -1,19 +1,74 @@
package main
import (
"context"
"fmt"
"log"
"net/http"
"os/signal"
"syscall"
"codeberg.org/arimelody/ari-stream-tools/config"
"codeberg.org/arimelody/ari-stream-tools/twitch"
"github.com/gin-gonic/gin"
)
/*
* -=[ SATELLITE ]=-
*
* a high-uptime companion service for stream-tools, tracking information that
* would be unwieldy to track all the time on an end device.
*
*
* current use-case: fetching and storing the latest twitch subscribers and
* cheers.
*
*
* should be able to utilise a bunch of existing code under /twitch, then just
* poke out an API for querying the information. SSE and websockets are
* unnecessary; stream-tools already has this. we just need a "kick-start" payload.
*/
func main() {
// TODO: build satellite service
failed := make(chan error)
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
gin.SetMode(gin.ReleaseMode)
cfg, err := config.LoadConfig()
if err != nil { log.Fatalf("Failed to load config: %v", err) }
srv := gin.New()
// srv.Use(gin.Logger())
srv.SetTrustedProxies([]string{"127.0.0.1", "::1"})
twitchService, err := twitch.New(ctx, twitch.ServiceOptions{
Host: cfg.Host,
Port: cfg.Port,
ChannelName: cfg.Twitch.ChannelName,
ClientID: cfg.Twitch.ClientID,
ClientSecret: cfg.Twitch.ClientSecret,
})
if err != nil {
log.Printf("Failed to create Twitch service: %v", err)
srv.Group("twitch").Any("/*any", func(ctx *gin.Context) {
ctx.String(http.StatusServiceUnavailable, http.StatusText(http.StatusServiceUnavailable))
})
} else {
// satellite should work fine with the existing twitch service, though
// local stream-tools will need some kind of API override.
twitchService.BindRoutes(srv.Group("twitch"))
go twitchService.Run(ctx)
}
go func() {
log.Printf("Now serving at http://%s:%d\n", cfg.Host, cfg.Port)
failed <- srv.Run(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
}()
select {
case err := <-failed:
log.Fatal(err)
case <-ctx.Done():
log.Print("Shutting down...")
}
}

View file

@ -4,14 +4,12 @@ import (
"context"
"fmt"
"log"
"math"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"codeberg.org/arimelody/ari-stream-tools/config"
"codeberg.org/arimelody/ari-stream-tools/learning"
"codeberg.org/arimelody/ari-stream-tools/music"
"codeberg.org/arimelody/ari-stream-tools/static"
@ -19,9 +17,6 @@ import (
"github.com/gin-gonic/gin"
)
var DEFAULT_HOST string = "0.0.0.0"
var DEFAULT_PORT int16 = 8080
func main() {
failed := make(chan error)
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
@ -29,19 +24,8 @@ func main() {
gin.SetMode(gin.ReleaseMode)
host := DEFAULT_HOST
port := DEFAULT_PORT
if len(os.Args) > 1 {
_port, err := strconv.Atoi(os.Args[1])
if _port > math.MaxUint16 {
log.Fatalf("Port must be between 1 and 65535: %d", _port)
}
port = int16(_port)
if err != nil {
log.Fatalf("Failed to parse port: %v", err)
}
}
cfg, err := config.LoadConfig()
if err != nil { log.Fatalf("Failed to load config: %v", err) }
srv := gin.New()
// srv.Use(gin.Logger())
@ -70,7 +54,13 @@ func main() {
go musicService.Run(ctx)
}
twitchService, err := twitch.New(ctx, twitch.ServiceOptions{ Port: port })
twitchService, err := twitch.New(ctx, twitch.ServiceOptions{
Host: cfg.Host,
Port: cfg.Port,
ChannelName: cfg.Twitch.ChannelName,
ClientID: cfg.Twitch.ClientID,
ClientSecret: cfg.Twitch.ClientSecret,
})
if err != nil {
log.Printf("Failed to create Twitch service: %v", err)
srv.Group("twitch").Any("/*any", func(ctx *gin.Context) {
@ -88,8 +78,8 @@ func main() {
})
go func() {
log.Printf("Now serving at http://%s:%d\n", host, port)
failed <- srv.Run(fmt.Sprintf("%s:%d", host, port))
log.Printf("Now serving at http://%s:%d\n", cfg.Host, cfg.Port)
failed <- srv.Run(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
}()
select {