Compare commits

..

3 commits

Author SHA1 Message Date
3995534ff4 format: use group var to define cli flags 2024-10-28 09:34:12 +01:00
7e39e47e99 format: update import statements order for consistency 2024-10-28 09:33:05 +01:00
75fda61f48 feat: update settings handling for pomodoro configuration
- add UpdateSettings function to manage pomodoro configuration
- refactor client command handling to use UpdateSettings function

🤖
2024-10-28 09:30:00 +01:00
7 changed files with 32 additions and 24 deletions

View file

@ -3,21 +3,25 @@ package server
import (
"flag"
"fmt"
"github.com/charmbracelet/log"
"net/http"
"os"
"git.smsvc.net/pomodoro/GoTomato/internal/metadata"
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
"git.smsvc.net/pomodoro/GoTomato/internal/websocket"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
"github.com/charmbracelet/log"
"net/http"
"os"
)
var (
// Define CLI flags
listenAddress = flag.String("listenAddress", shared.DefaultServerConfig.ListenAddress, "IP address to listen on")
listenPort = flag.Int("listenPort", shared.DefaultServerConfig.ListenPort, "Port to listen on")
password = flag.String("password", "", "Control password for pomodoro session (optional)")
showVersionFlag = flag.Bool("version", false, "Output version")
)
func Start() {
// Define CLI flags for ListenAddress and ListenPort
listenAddress := flag.String("listenAddress", shared.DefaultServerConfig.ListenAddress, "IP address to listen on")
listenPort := flag.Int("listenPort", shared.DefaultServerConfig.ListenPort, "Port to listen on")
password := flag.String("password", "", "Control password for pomodoro session (optional)")
showVersionFlag := flag.Bool("version", false, "Output version")
flag.Parse()
if *showVersionFlag {

View file

@ -1,9 +1,11 @@
package pomodoro
import (
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
"sync"
"time"
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
var mu sync.Mutex // to synchronize access to shared state
@ -99,3 +101,10 @@ func IsPomodoroPaused() bool {
defer mu.Unlock() // Ensures that the mutex is unlocked after the function is done
return shared.Message.Paused
}
func UpdateSettings(settings models.PomodoroConfig) {
if settings != (models.PomodoroConfig{}) {
shared.Message.Settings = settings
shared.Message.TimeLeft = settings.Work
}
}

View file

@ -1,8 +1,6 @@
package pomodoro
import (
"time"
)
import "time"
type Timer struct {
TimeLeft chan int // time left

View file

@ -1,8 +1,6 @@
package shared
import (
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
import "git.smsvc.net/pomodoro/GoTomato/pkg/models"
var DefaultServerConfig = models.ServerConfig{
ListenAddress: "0.0.0.0",

View file

@ -2,10 +2,11 @@ package websocket
import (
"encoding/json"
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
"github.com/charmbracelet/log"
"github.com/gorilla/websocket"
"time"
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
)
// sends continous messages to all connected WebSocket clients.

View file

@ -2,11 +2,12 @@ package websocket
import (
"encoding/json"
"github.com/charmbracelet/log"
"github.com/gorilla/websocket"
"git.smsvc.net/pomodoro/GoTomato/internal/pomodoro"
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
"github.com/charmbracelet/log"
"github.com/gorilla/websocket"
)
// handleClientCommands listens for commands from WebSocket clients
@ -49,11 +50,7 @@ func handleClientCommands(ws *websocket.Conn) {
}
case "updateSettings":
if !pomodoro.IsPomodoroOngoing() {
if clientCommand.Settings != (models.PomodoroConfig{}) {
shared.Message.Settings = clientCommand.Settings
shared.Message.TimeLeft = clientCommand.Settings.Work
}
pomodoro.UpdateSettings(clientCommand.Settings)
}
}
}

View file

@ -1,11 +1,12 @@
package websocket
import (
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
"github.com/charmbracelet/log"
"github.com/gorilla/websocket"
"net/http"
"sync"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
// Clients is a map of connected WebSocket clients, where each client is represented by the Client struct