diff --git a/main.go b/main.go index 2380ec12..e463f22b 100644 --- a/main.go +++ b/main.go @@ -10,12 +10,20 @@ import ( "time" ) -func updateSource(ctx context.Context) { +func updateSource(ctx context.Context, m3uUrl string, index int) { for { select { case <-ctx.Done(): return default: + fmt.Printf("Updating M3U #%s from %d\n", m3uUrl, index) + err := m3u.ParseM3UFromURL(m3uUrl, index) + if err != nil { + fmt.Printf("Error updating M3U: %v\n", err) + } + + fmt.Printf("Updated M3U #%s from %d\n", m3uUrl, index) + updateIntervalInHour, exists := os.LookupEnv("UPDATE_INTERVAL") if !exists { updateIntervalInHour = "24" @@ -27,17 +35,15 @@ func updateSource(ctx context.Context) { } else { time.Sleep(time.Duration(hourInt) * time.Hour) // Adjust the update interval as needed } - - // Reload M3U files - // err = m3u.GetStreams(true) - // if err != nil { - // fmt.Printf("Error updating M3U: %v\n", err) - // } } } } func main() { + // Context for graceful shutdown + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + index := 1 for { m3uUrl, m3uExists := os.LookupEnv(fmt.Sprintf("M3U_URL_%d", index)) @@ -45,19 +51,11 @@ func main() { break } - err := m3u.ParseM3UFromURL(m3uUrl, index) - if err != nil { - - } + // Start the goroutine for periodic updates + go updateSource(ctx, m3uUrl, index) index++ } - // Context for graceful shutdown - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - // Start the goroutine for periodic updates - go updateSource(ctx) // HTTP handlers http.HandleFunc("/playlist.m3u", m3u.GenerateM3UContent)