feat(server): restructure Pomodoro server into modular components
- move server logic to cmd/server/main.go
- create packages for websocket, pomodoro and broadcast handling
- define models for messages
- remove old GoTomato.go file
- update README
🤖
This commit is contained in:
parent
6d73711341
commit
c59f737eb7
10 changed files with 224 additions and 157 deletions
28
internal/broadcast/broadcast.go
Normal file
28
internal/broadcast/broadcast.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package broadcast
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
"github.com/gorilla/websocket"
|
||||
"log"
|
||||
)
|
||||
|
||||
// BroadcastMessage sends a message to all connected WebSocket clients.
|
||||
func BroadcastMessage(clients map[*websocket.Conn]bool, message models.BroadcastMessage) {
|
||||
// Marshal the message into JSON format
|
||||
jsonMessage, err := json.Marshal(message)
|
||||
if err != nil {
|
||||
log.Printf("Error marshalling message: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Iterate over all connected clients and broadcast the message
|
||||
for client := range clients {
|
||||
err := client.WriteMessage(websocket.TextMessage, jsonMessage)
|
||||
if err != nil {
|
||||
log.Printf("Error broadcasting to client: %v", err)
|
||||
client.Close()
|
||||
delete(clients, client) // Remove the client if an error occurs
|
||||
}
|
||||
}
|
||||
}
|
39
internal/pomodoro/pomodoro.go
Normal file
39
internal/pomodoro/pomodoro.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
package pomodoro
|
||||
|
||||
import (
|
||||
"github.com/gorilla/websocket"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
workDuration = 15 * 60
|
||||
shortBreakDuration = 5 * 60
|
||||
longBreakDuration = 10 * 60
|
||||
sessions = 4
|
||||
)
|
||||
|
||||
var mu sync.Mutex // to synchronize access to shared state
|
||||
|
||||
// RunPomodoroTimer iterates the Pomodoro work/break sessions.
|
||||
func RunPomodoroTimer(clients map[*websocket.Conn]bool) {
|
||||
mu.Lock()
|
||||
timerRunning = true
|
||||
|
||||
for session := 1; session <= sessions; session++ {
|
||||
if !startTimer(clients, workDuration, "Work", session) {
|
||||
break
|
||||
}
|
||||
if session == sessions {
|
||||
if !startTimer(clients, longBreakDuration, "LongBreak", session) {
|
||||
break
|
||||
}
|
||||
} else {
|
||||
if !startTimer(clients, shortBreakDuration, "ShortBreak", session) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
timerRunning = false
|
||||
mu.Unlock()
|
||||
}
|
51
internal/pomodoro/timer.go
Normal file
51
internal/pomodoro/timer.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package pomodoro
|
||||
|
||||
import (
|
||||
"git.smsvc.net/pomodoro/GoTomato/internal/broadcast"
|
||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
"github.com/gorilla/websocket"
|
||||
"time"
|
||||
)
|
||||
|
||||
var timerRunning bool
|
||||
var timerStopChannel = make(chan bool, 1)
|
||||
|
||||
// startTimer runs the countdown and broadcasts every second.
|
||||
func startTimer(clients map[*websocket.Conn]bool, remainingSeconds int, mode string, session int) bool {
|
||||
for remainingSeconds > 0 {
|
||||
select {
|
||||
case <-timerStopChannel:
|
||||
return false // Stop the timer if a stop command is received
|
||||
default:
|
||||
// Broadcast the current state to all clients
|
||||
broadcast.BroadcastMessage(clients, models.BroadcastMessage{
|
||||
Mode: mode,
|
||||
Session: session,
|
||||
MaxSession: sessions,
|
||||
TimeLeft: remainingSeconds,
|
||||
})
|
||||
time.Sleep(time.Second)
|
||||
remainingSeconds--
|
||||
}
|
||||
}
|
||||
|
||||
// Final broadcast when time reaches zero
|
||||
broadcast.BroadcastMessage(clients, models.BroadcastMessage{
|
||||
Mode: mode,
|
||||
Session: session,
|
||||
MaxSession: sessions,
|
||||
TimeLeft: 0,
|
||||
})
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// StopTimer sends a signal to stop the running Pomodoro timer.
|
||||
func StopTimer() {
|
||||
timerStopChannel <- true
|
||||
}
|
||||
|
||||
// IsTimerRunning returns the status of the timer.
|
||||
func IsTimerRunning() bool {
|
||||
return timerRunning
|
||||
}
|
41
internal/websocket/client_commands.go
Normal file
41
internal/websocket/client_commands.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
package websocket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"git.smsvc.net/pomodoro/GoTomato/internal/pomodoro"
|
||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
"github.com/gorilla/websocket"
|
||||
"log"
|
||||
)
|
||||
|
||||
// handleClientCommands listens for commands from WebSocket clients and dispatches to the timer.
|
||||
func handleClientCommands(ws *websocket.Conn) {
|
||||
for {
|
||||
_, message, err := ws.ReadMessage()
|
||||
if err != nil {
|
||||
log.Printf("Client disconnected: %v", err)
|
||||
delete(Clients, ws)
|
||||
break
|
||||
}
|
||||
|
||||
// Handle incoming commands
|
||||
var command models.ClientCommand
|
||||
err = json.Unmarshal(message, &command)
|
||||
if err != nil {
|
||||
log.Printf("Error unmarshalling command: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Process the command
|
||||
switch command.Command {
|
||||
case "start":
|
||||
if !pomodoro.IsTimerRunning() {
|
||||
go pomodoro.RunPomodoroTimer(Clients) // Start the timer with the list of clients
|
||||
}
|
||||
case "stop":
|
||||
if pomodoro.IsTimerRunning() {
|
||||
pomodoro.StopTimer() // Stop the timer in the Pomodoro package
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
32
internal/websocket/handle_connections.go
Normal file
32
internal/websocket/handle_connections.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
package websocket
|
||||
|
||||
import (
|
||||
"github.com/gorilla/websocket"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Map to track connected clients
|
||||
var Clients = make(map[*websocket.Conn]bool)
|
||||
|
||||
// Upgrader to upgrade HTTP requests to WebSocket connections
|
||||
var upgrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool { return true },
|
||||
}
|
||||
|
||||
// HandleConnections upgrades HTTP requests to WebSocket connections and manages the client lifecycle.
|
||||
func HandleConnections(w http.ResponseWriter, r *http.Request) {
|
||||
// Upgrade initial GET request to a WebSocket
|
||||
ws, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
log.Printf("WebSocket upgrade error: %v", err)
|
||||
return
|
||||
}
|
||||
defer ws.Close()
|
||||
|
||||
// Register the new client
|
||||
Clients[ws] = true
|
||||
|
||||
// Listen for commands from the connected client
|
||||
handleClientCommands(ws)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue