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-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-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-20 12:55:02 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
serverConfig := models.GoTomatoServerConfig{
|
|
|
|
ListenAddress: *listenAddress,
|
|
|
|
ListenPort: *listenPort,
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|