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() { 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...") } }