From a9d145ee713b2b3511fc5e7c61064c8ca7c96359 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Mon, 21 Oct 2024 09:46:16 +0200 Subject: [PATCH] feat: create shared config defaults MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - add shared configuration defaults for server and pomodoro 🤖 --- cmd/server/main.go | 5 +++-- internal/shared/configDefaults.go | 20 ++++++++++++++++++++ internal/websocket/client_commands.go | 12 +++--------- 3 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 internal/shared/configDefaults.go diff --git a/cmd/server/main.go b/cmd/server/main.go index b1d9db8..9ce6580 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -3,6 +3,7 @@ package server import ( "flag" "fmt" + "git.smsvc.net/pomodoro/GoTomato/internal/shared" "git.smsvc.net/pomodoro/GoTomato/internal/websocket" "git.smsvc.net/pomodoro/GoTomato/pkg/models" "log" @@ -11,8 +12,8 @@ import ( func Start() { // Define CLI flags for ListenAddress and ListenPort - listenAddress := flag.String("listenAddress", "0.0.0.0", "IP address to listen on") - listenPort := flag.Int("listenPort", 8080, "Port to listen on") + listenAddress := flag.String("listenAddress", shared.DefaultServerConfig.ListenAddress, "IP address to listen on") + listenPort := flag.Int("listenPort", shared.DefaultServerConfig.ListenPort, "Port to listen on") flag.Parse() serverConfig := models.GoTomatoServerConfig{ diff --git a/internal/shared/configDefaults.go b/internal/shared/configDefaults.go new file mode 100644 index 0000000..ba4be6c --- /dev/null +++ b/internal/shared/configDefaults.go @@ -0,0 +1,20 @@ +package shared + +import ( + "git.smsvc.net/pomodoro/GoTomato/pkg/models" +) + +var DefaultServerConfig = models.GoTomatoServerConfig{ + ListenAddress: "0.0.0.0", + ListenPort: 8080, +} + +var DefaultPomodoroConfig = models.GoTomatoPomodoroConfig{ + Work: 25 * 60, + ShortBreak: 5 * 60, + LongBreak: 15 * 60, + Sessions: 4, +} + +// used to check if client passed a config json +var UnsetPomodoroConfig models.GoTomatoPomodoroConfig diff --git a/internal/websocket/client_commands.go b/internal/websocket/client_commands.go index 17560aa..e1ff8b0 100644 --- a/internal/websocket/client_commands.go +++ b/internal/websocket/client_commands.go @@ -3,23 +3,17 @@ package websocket import ( "encoding/json" "git.smsvc.net/pomodoro/GoTomato/internal/pomodoro" + "git.smsvc.net/pomodoro/GoTomato/internal/shared" "git.smsvc.net/pomodoro/GoTomato/pkg/models" "github.com/gorilla/websocket" "log" ) -var unsetPomodoroConfig models.GoTomatoPomodoroConfig // used to check if client passed a config json - // handleClientCommands listens for commands from WebSocket clients func handleClientCommands(ws *websocket.Conn) { for { var clientCommand models.ClientCommand - var pomodoroConfig = models.GoTomatoPomodoroConfig{ - Work: 25 * 60, - ShortBreak: 5 * 60, - LongBreak: 15 * 60, - Sessions: 4, - } + var pomodoroConfig = shared.DefaultPomodoroConfig _, message, err := ws.ReadMessage() if err != nil { @@ -39,7 +33,7 @@ func handleClientCommands(ws *websocket.Conn) { switch clientCommand.Command { case "start": if !pomodoro.IsPomodoroOngoing() { - if clientCommand.Config != unsetPomodoroConfig { + if clientCommand.Config != shared.UnsetPomodoroConfig { pomodoroConfig = clientCommand.Config } go pomodoro.RunPomodoro(pomodoroConfig) // Start the timer with the list of clients