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 06:16:26 +00:00
|
|
|
"git.smsvc.net/pomodoro/GoTomato/internal/broadcast"
|
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
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
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 06:16:26 +00:00
|
|
|
go broadcast.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)
|
|
|
|
}
|
|
|
|
}
|