Sebastian Mark
44a64bfce4
- add Gorilla handlers package for enhanced HTTP handling - refactor HTTP server to use a new ServeMux for routing - update ListenAndServe to utilize ProxyHeaders for better proxy support - add RealIP field to client model - use RealIP fild for connect/disconnect log output
30 lines
892 B
Go
30 lines
892 B
Go
package models
|
|
|
|
import (
|
|
"github.com/charmbracelet/log"
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
// Represents a command from the client (start/stop)
|
|
type ClientCommand struct {
|
|
Command string `json:"command"` // Command send to the server
|
|
Password string `json:"password"` // Pomodoro control password
|
|
Settings PomodoroConfig `json:"settings"` // Pomodoro config
|
|
}
|
|
|
|
// Represents a single client
|
|
type WebsocketClient struct {
|
|
Conn *websocket.Conn
|
|
RealIP string
|
|
}
|
|
|
|
// Sends a message to the websocket.
|
|
// Automatically locks and unlocks the client mutex, to ensure that only one goroutine can write at a time.
|
|
func (c *WebsocketClient) SendMessage(messageType int, data []byte) error {
|
|
err := c.Conn.WriteMessage(messageType, data)
|
|
if err != nil {
|
|
log.Error("Error writing to WebSocket:", "msg", err)
|
|
c.Conn.Close() // Close the connection on error
|
|
}
|
|
return err
|
|
}
|