Compare commits
3 commits
3d5cb29c54
...
0b2f83cd35
Author | SHA1 | Date | |
---|---|---|---|
0b2f83cd35 | |||
85923b4469 | |||
09b475dd61 |
5 changed files with 47 additions and 15 deletions
|
@ -6,6 +6,10 @@ A pomodoro server written in Go
|
||||||
|
|
||||||
`go install git.smsvc.net/pomodoro/GoTomato@latest`
|
`go install git.smsvc.net/pomodoro/GoTomato@latest`
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
See `GoTomato --help` for Parameters
|
||||||
|
|
||||||
## Testing
|
## Testing
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
|
@ -1,16 +1,31 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
"git.smsvc.net/pomodoro/GoTomato/internal/websocket"
|
"git.smsvc.net/pomodoro/GoTomato/internal/websocket"
|
||||||
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Start() {
|
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")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
serverConfig := models.GoTomatoServerConfig{
|
||||||
|
ListenAddress: *listenAddress,
|
||||||
|
ListenPort: *listenPort,
|
||||||
|
}
|
||||||
|
|
||||||
|
listen := fmt.Sprintf("%s:%d", serverConfig.ListenAddress, serverConfig.ListenPort)
|
||||||
|
|
||||||
http.HandleFunc("/ws", websocket.HandleConnections)
|
http.HandleFunc("/ws", websocket.HandleConnections)
|
||||||
|
|
||||||
log.Println("Pomodoro WebSocket server started on :8080")
|
log.Printf("Pomodoro WebSocket server started on %s\n", listen)
|
||||||
err := http.ListenAndServe(":8080", nil)
|
err := http.ListenAndServe(listen, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error starting server: %v", err)
|
log.Fatalf("Error starting server: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,12 +7,12 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
var PomodoroConfig = models.GoTomatoTimerConfig{
|
||||||
workDuration = 15 * 60
|
Work: 15 * 60,
|
||||||
shortBreakDuration = 5 * 60
|
ShortBreak: 5 * 60,
|
||||||
longBreakDuration = 10 * 60
|
LongBreak: 10 * 60,
|
||||||
sessions = 4
|
Sessions: 4,
|
||||||
)
|
}
|
||||||
|
|
||||||
var pomodoroRunning bool
|
var pomodoroRunning bool
|
||||||
var pomodoroPaused bool
|
var pomodoroPaused bool
|
||||||
|
@ -30,16 +30,16 @@ func RunPomodoroTimer(clients map[*websocket.Conn]*models.Client) {
|
||||||
pomodoroPaused = false
|
pomodoroPaused = false
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
|
|
||||||
for session := 1; session <= sessions; session++ {
|
for session := 1; session <= PomodoroConfig.Sessions; session++ {
|
||||||
if !startTimer(clients, workDuration, "Work", session) {
|
if !startTimer(clients, PomodoroConfig.Work, "Work", session) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if session == sessions {
|
if session == PomodoroConfig.Sessions {
|
||||||
if !startTimer(clients, longBreakDuration, "LongBreak", session) {
|
if !startTimer(clients, PomodoroConfig.LongBreak, "LongBreak", session) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if !startTimer(clients, shortBreakDuration, "ShortBreak", session) {
|
if !startTimer(clients, PomodoroConfig.ShortBreak, "ShortBreak", session) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ func startTimer(clients map[*websocket.Conn]*models.Client, remainingSeconds int
|
||||||
broadcast.BroadcastMessage(clients, models.BroadcastMessage{
|
broadcast.BroadcastMessage(clients, models.BroadcastMessage{
|
||||||
Mode: mode,
|
Mode: mode,
|
||||||
Session: session,
|
Session: session,
|
||||||
MaxSession: sessions,
|
MaxSession: PomodoroConfig.Sessions,
|
||||||
TimeLeft: remainingSeconds,
|
TimeLeft: remainingSeconds,
|
||||||
})
|
})
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
@ -36,7 +36,7 @@ func startTimer(clients map[*websocket.Conn]*models.Client, remainingSeconds int
|
||||||
broadcast.BroadcastMessage(clients, models.BroadcastMessage{
|
broadcast.BroadcastMessage(clients, models.BroadcastMessage{
|
||||||
Mode: mode,
|
Mode: mode,
|
||||||
Session: session,
|
Session: session,
|
||||||
MaxSession: sessions,
|
MaxSession: PomodoroConfig.Sessions,
|
||||||
TimeLeft: 0,
|
TimeLeft: 0,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
13
pkg/models/config.go
Normal file
13
pkg/models/config.go
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
package models
|
||||||
|
|
||||||
|
type GoTomatoTimerConfig struct {
|
||||||
|
Work int `json:"work"`
|
||||||
|
ShortBreak int `json:"shortBreak"`
|
||||||
|
LongBreak int `json:"longBreak"`
|
||||||
|
Sessions int `json:"sessions"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GoTomatoServerConfig struct {
|
||||||
|
ListenAddress string `json:"listenAddress, default:"0.0.0.0"`
|
||||||
|
ListenPort int `json:"listenPort, default:8080"`
|
||||||
|
}
|
Loading…
Reference in a new issue