From 06bedb28adbfa0fa8ddaa73fca4fd52fed746336 Mon Sep 17 00:00:00 2001 From: client-side96 Date: Thu, 30 May 2024 15:40:02 +0200 Subject: [PATCH] feat: configure publish interval with command line flag --- cmd/pi-monitor/main.go | 20 +++++++++++++------- internal/config/environment.go | 5 +++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/cmd/pi-monitor/main.go b/cmd/pi-monitor/main.go index c930e34..08e5b45 100644 --- a/cmd/pi-monitor/main.go +++ b/cmd/pi-monitor/main.go @@ -17,10 +17,11 @@ import ( var scriptDir string var addr string +var publishInterval int -func mainLoop(statsService *service.StatsService) { +func mainLoop(env config.Environment, statsService *service.StatsService) { - ticker := time.NewTicker(1 * time.Second) + ticker := time.NewTicker(time.Duration(env.PublishInterval) * time.Second) done := make(chan bool) defer func() { @@ -36,8 +37,6 @@ func mainLoop(statsService *service.StatsService) { statsService.PublishToAllClients() case client := <-statsService.Channel: statsService.HandleStatsSubscripition(client) - // default: - } } @@ -56,11 +55,18 @@ func main() { ":8000", "Port the webserver runs on", ) + flag.IntVar( + &publishInterval, + "publish-interval", + 1, + "Interval in seconds in which the statistics are published to clients", + ) flag.Parse() env := config.Environment{ - ScriptDir: scriptDir, - Addr: addr, + ScriptDir: scriptDir, + Addr: addr, + PublishInterval: publishInterval, } // Low level @@ -80,7 +86,7 @@ func main() { router := api.NewRouter(statsHandler) - go mainLoop(statsService) + go mainLoop(env, statsService) httpServer := &http.Server{ Addr: env.Addr, diff --git a/internal/config/environment.go b/internal/config/environment.go index 0c3b5b8..b99a1d9 100644 --- a/internal/config/environment.go +++ b/internal/config/environment.go @@ -1,6 +1,7 @@ package config type Environment struct { - ScriptDir string - Addr string + ScriptDir string + Addr string + PublishInterval int }