Compare commits
No commits in common. "d7b77890512385b8c17df8838eaa8f5b9f201ffd" and "76f395429978bc46340dd081746b7065df0577ca" have entirely different histories.
d7b7789051
...
76f3954299
7 changed files with 18 additions and 31 deletions
|
@ -3,11 +3,11 @@ package server
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/charmbracelet/log"
|
||||||
"github.com/gorilla/handlers"
|
"github.com/gorilla/handlers"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"git.smsvc.net/pomodoro/GoTomato/internal/helper"
|
|
||||||
"git.smsvc.net/pomodoro/GoTomato/internal/metadata"
|
"git.smsvc.net/pomodoro/GoTomato/internal/metadata"
|
||||||
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
|
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
|
||||||
"git.smsvc.net/pomodoro/GoTomato/internal/websocket"
|
"git.smsvc.net/pomodoro/GoTomato/internal/websocket"
|
||||||
|
@ -48,12 +48,12 @@ func Start() {
|
||||||
r.HandleFunc("/ws", websocket.HandleConnection)
|
r.HandleFunc("/ws", websocket.HandleConnection)
|
||||||
go websocket.SendPermanentBroadCastMessage()
|
go websocket.SendPermanentBroadCastMessage()
|
||||||
|
|
||||||
helper.Logger.Info("GoTomato started", "version", metadata.GoTomatoVersion)
|
log.Info("GoTomato started", "version", metadata.GoTomatoVersion)
|
||||||
helper.Logger.Info("Websocket listening", "address", listen)
|
log.Info("Websocket listening", "address", listen)
|
||||||
|
|
||||||
// start the listener
|
// start the listener
|
||||||
err := http.ListenAndServe(listen, handlers.ProxyHeaders(r))
|
err := http.ListenAndServe(listen, handlers.ProxyHeaders(r))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helper.Logger.Fatal("Error starting server:", "msg", err)
|
log.Fatal("Error starting server:", "msg", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
package helper
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/charmbracelet/log"
|
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
var Logger = log.NewWithOptions(os.Stdout, log.Options{
|
|
||||||
ReportTimestamp: true,
|
|
||||||
TimeFormat: time.RFC3339,
|
|
||||||
})
|
|
|
@ -2,5 +2,5 @@ package metadata
|
||||||
|
|
||||||
import "strings"
|
import "strings"
|
||||||
|
|
||||||
const GoTomatoVersion = "v0.0.8" // The GoTomato version
|
const GoTomatoVersion = "v0.0.7" // The GoTomato version
|
||||||
var ProtocolVersion = strings.Split(GoTomatoVersion, ".")[0] // The protocol version
|
var ProtocolVersion = strings.Split(GoTomatoVersion, ".")[0] // The protocol version
|
||||||
|
|
|
@ -2,10 +2,10 @@ package websocket
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/charmbracelet/log"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.smsvc.net/pomodoro/GoTomato/internal/helper"
|
|
||||||
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
|
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -16,14 +16,14 @@ func SendPermanentBroadCastMessage() {
|
||||||
// Marshal the message into JSON format
|
// Marshal the message into JSON format
|
||||||
jsonMessage, err := json.Marshal(shared.State)
|
jsonMessage, err := json.Marshal(shared.State)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helper.Logger.Error("Error marshalling message:", "msg", err)
|
log.Error("Error marshalling message:", "msg", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Iterate over all connected clients and broadcast the message
|
// Iterate over all connected clients and broadcast the message
|
||||||
for _, client := range Clients {
|
for _, client := range Clients {
|
||||||
err := client.SendMessage(websocket.TextMessage, jsonMessage)
|
err := client.SendMessage(websocket.TextMessage, jsonMessage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helper.Logger.Error("Error broadcasting to client:", "msg", err)
|
log.Error("Error broadcasting to client:", "msg", err)
|
||||||
// The client is responsible for closing itself on error
|
// The client is responsible for closing itself on error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,8 @@ package websocket
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/charmbracelet/log"
|
||||||
|
|
||||||
"git.smsvc.net/pomodoro/GoTomato/internal/helper"
|
|
||||||
"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/internal/shared"
|
||||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
|
@ -22,14 +22,14 @@ func handleClientCommands(c models.WebsocketClient) {
|
||||||
_, message, err := ws.ReadMessage()
|
_, message, err := ws.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
delete(Clients, ws.LocalAddr())
|
delete(Clients, ws.LocalAddr())
|
||||||
helper.Logger.Info("Client disconnected:", "msg", err, "host", c.RealIP, "clients", len(Clients))
|
log.Info("Client disconnected:", "msg", err, "host", c.RealIP, "clients", len(Clients))
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle incoming commands
|
// Handle incoming commands
|
||||||
err = json.Unmarshal(message, &clientCommand)
|
err = json.Unmarshal(message, &clientCommand)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helper.Logger.Error("Error unmarshalling command:", "msg", err)
|
log.Error("Error unmarshalling command:", "msg", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,10 +55,10 @@ func handleClientCommands(c models.WebsocketClient) {
|
||||||
case "updateSettings":
|
case "updateSettings":
|
||||||
if !pomodoro.IsPomodoroOngoing() {
|
if !pomodoro.IsPomodoroOngoing() {
|
||||||
if !checkSettings(clientCommand.Settings) {
|
if !checkSettings(clientCommand.Settings) {
|
||||||
helper.Logger.Warn("Ignoring invalid config:", "msg", clientCommand.Settings, "host", c.Conn.RemoteAddr())
|
log.Warn("Ignoring invalid config:", "msg", clientCommand.Settings, "host", c.Conn.RemoteAddr())
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
helper.Logger.Info("Client send config", "config", clientCommand.Settings, "host", c.Conn.RemoteAddr())
|
log.Info("Client send config", "config", clientCommand.Settings, "host", c.Conn.RemoteAddr())
|
||||||
pomodoro.UpdateSettings(clientCommand.Settings)
|
pomodoro.UpdateSettings(clientCommand.Settings)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
package websocket
|
package websocket
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/charmbracelet/log"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"git.smsvc.net/pomodoro/GoTomato/internal/helper"
|
|
||||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ func HandleConnection(w http.ResponseWriter, r *http.Request) {
|
||||||
// Upgrade initial GET request to a WebSocket
|
// Upgrade initial GET request to a WebSocket
|
||||||
ws, err := upgrader.Upgrade(w, r, nil)
|
ws, err := upgrader.Upgrade(w, r, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helper.Logger.Error("WebSocket upgrade error:", "msg", err)
|
log.Error("WebSocket upgrade error:", "msg", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer ws.Close()
|
defer ws.Close()
|
||||||
|
@ -38,7 +38,7 @@ func HandleConnection(w http.ResponseWriter, r *http.Request) {
|
||||||
Clients[ws.LocalAddr()] = &client
|
Clients[ws.LocalAddr()] = &client
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
|
|
||||||
helper.Logger.Info("Client connected", "host", client.RealIP, "clients", len(Clients))
|
log.Info("Client connected", "host", client.RealIP, "clients", len(Clients))
|
||||||
|
|
||||||
// Listen for commands from the connected client
|
// Listen for commands from the connected client
|
||||||
handleClientCommands(client)
|
handleClientCommands(client)
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/charmbracelet/log"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
|
||||||
"git.smsvc.net/pomodoro/GoTomato/internal/helper"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Represents a command from the client (start/stop)
|
// Represents a command from the client (start/stop)
|
||||||
|
@ -24,7 +23,7 @@ type WebsocketClient struct {
|
||||||
func (c *WebsocketClient) SendMessage(messageType int, data []byte) error {
|
func (c *WebsocketClient) SendMessage(messageType int, data []byte) error {
|
||||||
err := c.Conn.WriteMessage(messageType, data)
|
err := c.Conn.WriteMessage(messageType, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helper.Logger.Error("Error writing to WebSocket:", "msg", err)
|
log.Error("Error writing to WebSocket:", "msg", err)
|
||||||
c.Conn.Close() // Close the connection on error
|
c.Conn.Close() // Close the connection on error
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Reference in a new issue