2024-10-19 10:05:10 +00:00
|
|
|
package server
|
2024-10-19 09:47:56 +00:00
|
|
|
|
|
|
|
import (
|
2024-10-20 12:55:02 +00:00
|
|
|
"flag"
|
2024-10-20 12:31:52 +00:00
|
|
|
"fmt"
|
2024-10-22 15:18:05 +00:00
|
|
|
"git.smsvc.net/pomodoro/GoTomato/internal/metadata"
|
2024-10-21 07:46:16 +00:00
|
|
|
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
|
2024-10-19 09:47:56 +00:00
|
|
|
"git.smsvc.net/pomodoro/GoTomato/internal/websocket"
|
2024-10-20 12:31:52 +00:00
|
|
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
2024-10-19 09:47:56 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-10-22 15:18:05 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2024-10-19 09:47:56 +00:00
|
|
|
)
|
|
|
|
|
2024-10-19 10:05:10 +00:00
|
|
|
func Start() {
|
2024-10-20 12:55:02 +00:00
|
|
|
// Define CLI flags for ListenAddress and ListenPort
|
2024-10-21 07:46:16 +00:00
|
|
|
listenAddress := flag.String("listenAddress", shared.DefaultServerConfig.ListenAddress, "IP address to listen on")
|
|
|
|
listenPort := flag.Int("listenPort", shared.DefaultServerConfig.ListenPort, "Port to listen on")
|
2024-10-21 13:36:26 +00:00
|
|
|
password := flag.String("password", "", "Control password for pomodoro session (optional)")
|
2024-10-22 15:18:05 +00:00
|
|
|
showVersionFlag := flag.Bool("version", false, "Output version")
|
2024-10-20 12:55:02 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2024-10-22 15:18:05 +00:00
|
|
|
shared.Message.GoTomatoVersion = strings.Split(metadata.GoTomatoVersion, ".")[0]
|
|
|
|
if *showVersionFlag {
|
|
|
|
fmt.Printf("App-Version: %s\n", metadata.GoTomatoVersion)
|
|
|
|
fmt.Printf("Protocol-Version: %s\n", shared.Message.GoTomatoVersion)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2024-10-20 12:55:02 +00:00
|
|
|
serverConfig := models.GoTomatoServerConfig{
|
|
|
|
ListenAddress: *listenAddress,
|
|
|
|
ListenPort: *listenPort,
|
|
|
|
}
|
2024-10-21 13:36:26 +00:00
|
|
|
shared.PomodoroPassword = *password
|
2024-10-20 12:55:02 +00:00
|
|
|
|
2024-10-20 12:31:52 +00:00
|
|
|
listen := fmt.Sprintf("%s:%d", serverConfig.ListenAddress, serverConfig.ListenPort)
|
|
|
|
|
2024-10-19 09:47:56 +00:00
|
|
|
http.HandleFunc("/ws", websocket.HandleConnections)
|
2024-10-21 07:24:20 +00:00
|
|
|
go websocket.SendPermanentBroadCastMessage()
|
2024-10-19 09:47:56 +00:00
|
|
|
|
2024-10-20 12:31:52 +00:00
|
|
|
log.Printf("Pomodoro WebSocket server started on %s\n", listen)
|
|
|
|
err := http.ListenAndServe(listen, nil)
|
2024-10-19 09:47:56 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error starting server: %v", err)
|
|
|
|
}
|
|
|
|
}
|