feat: create shared config defaults

- add shared configuration defaults for server and pomodoro

🤖
This commit is contained in:
Sebastian Mark 2024-10-21 09:46:16 +02:00
parent 28342058aa
commit a9d145ee71
3 changed files with 26 additions and 11 deletions

View file

@ -3,6 +3,7 @@ package server
import ( import (
"flag" "flag"
"fmt" "fmt"
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
"git.smsvc.net/pomodoro/GoTomato/internal/websocket" "git.smsvc.net/pomodoro/GoTomato/internal/websocket"
"git.smsvc.net/pomodoro/GoTomato/pkg/models" "git.smsvc.net/pomodoro/GoTomato/pkg/models"
"log" "log"
@ -11,8 +12,8 @@ import (
func Start() { func Start() {
// Define CLI flags for ListenAddress and ListenPort // Define CLI flags for ListenAddress and ListenPort
listenAddress := flag.String("listenAddress", "0.0.0.0", "IP address to listen on") listenAddress := flag.String("listenAddress", shared.DefaultServerConfig.ListenAddress, "IP address to listen on")
listenPort := flag.Int("listenPort", 8080, "Port to listen on") listenPort := flag.Int("listenPort", shared.DefaultServerConfig.ListenPort, "Port to listen on")
flag.Parse() flag.Parse()
serverConfig := models.GoTomatoServerConfig{ serverConfig := models.GoTomatoServerConfig{

View file

@ -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

View file

@ -3,23 +3,17 @@ package websocket
import ( import (
"encoding/json" "encoding/json"
"git.smsvc.net/pomodoro/GoTomato/internal/pomodoro" "git.smsvc.net/pomodoro/GoTomato/internal/pomodoro"
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
"git.smsvc.net/pomodoro/GoTomato/pkg/models" "git.smsvc.net/pomodoro/GoTomato/pkg/models"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"log" "log"
) )
var unsetPomodoroConfig models.GoTomatoPomodoroConfig // used to check if client passed a config json
// handleClientCommands listens for commands from WebSocket clients // handleClientCommands listens for commands from WebSocket clients
func handleClientCommands(ws *websocket.Conn) { func handleClientCommands(ws *websocket.Conn) {
for { for {
var clientCommand models.ClientCommand var clientCommand models.ClientCommand
var pomodoroConfig = models.GoTomatoPomodoroConfig{ var pomodoroConfig = shared.DefaultPomodoroConfig
Work: 25 * 60,
ShortBreak: 5 * 60,
LongBreak: 15 * 60,
Sessions: 4,
}
_, message, err := ws.ReadMessage() _, message, err := ws.ReadMessage()
if err != nil { if err != nil {
@ -39,7 +33,7 @@ func handleClientCommands(ws *websocket.Conn) {
switch clientCommand.Command { switch clientCommand.Command {
case "start": case "start":
if !pomodoro.IsPomodoroOngoing() { if !pomodoro.IsPomodoroOngoing() {
if clientCommand.Config != unsetPomodoroConfig { if clientCommand.Config != shared.UnsetPomodoroConfig {
pomodoroConfig = clientCommand.Config pomodoroConfig = clientCommand.Config
} }
go pomodoro.RunPomodoro(pomodoroConfig) // Start the timer with the list of clients go pomodoro.RunPomodoro(pomodoroConfig) // Start the timer with the list of clients