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-11-03 09:00:14 +00:00
|
|
|
"github.com/gorilla/handlers"
|
2024-10-28 08:33:05 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2024-11-04 19:33:25 +00:00
|
|
|
"git.smsvc.net/pomodoro/GoTomato/internal/helper"
|
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
|
|
|
)
|
|
|
|
|
2024-10-28 08:34:12 +00:00
|
|
|
var (
|
2024-10-30 06:37:14 +00:00
|
|
|
// define CLI flags
|
2024-11-18 07:11:39 +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")
|
|
|
|
password = flag.String("password", "", "Control password for pomodoro session (optional)")
|
|
|
|
showVersion = flag.Bool("version", false, "Output version")
|
2024-10-28 08:34:12 +00:00
|
|
|
)
|
|
|
|
|
2024-10-30 06:37:14 +00:00
|
|
|
// Start the pomodoro server
|
2024-10-19 10:05:10 +00:00
|
|
|
func Start() {
|
2024-11-18 07:57:32 +00:00
|
|
|
// Update usage output
|
|
|
|
flag.Usage = func() {
|
|
|
|
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", "GoTomato")
|
|
|
|
flag.PrintDefaults()
|
2024-11-18 16:43:49 +00:00
|
|
|
fmt.Println("\nPoint your client to 'ws://<listenAddress>:<listenPort>'")
|
2024-11-18 07:57:32 +00:00
|
|
|
}
|
|
|
|
|
2024-10-20 12:55:02 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2024-10-30 06:37:14 +00:00
|
|
|
// show server and protocl version and exit
|
2024-11-18 07:11:39 +00:00
|
|
|
if *showVersion {
|
2024-11-08 13:22:54 +00:00
|
|
|
fmt.Printf("GoTomato v%s\n", metadata.GoTomatoVersion)
|
|
|
|
fmt.Printf("Protocol-Version: %s\n", metadata.ProtocolVersion)
|
2024-10-22 15:18:05 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2024-10-30 06:37:14 +00:00
|
|
|
// set server config
|
2024-10-26 07:22:21 +00:00
|
|
|
serverConfig := models.ServerConfig{
|
2024-10-20 12:55:02 +00:00
|
|
|
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-30 06:37:14 +00:00
|
|
|
// define listen address for websocket
|
2024-10-20 12:31:52 +00:00
|
|
|
listen := fmt.Sprintf("%s:%d", serverConfig.ListenAddress, serverConfig.ListenPort)
|
|
|
|
|
2024-10-30 06:37:14 +00:00
|
|
|
// start connection handler and broadcast
|
2024-11-03 09:00:14 +00:00
|
|
|
r := http.NewServeMux()
|
2024-11-18 16:43:49 +00:00
|
|
|
r.HandleFunc("/", websocket.HandleConnection)
|
2024-10-21 07:24:20 +00:00
|
|
|
go websocket.SendPermanentBroadCastMessage()
|
2024-10-19 09:47:56 +00:00
|
|
|
|
2024-11-04 19:33:25 +00:00
|
|
|
helper.Logger.Info("GoTomato started", "version", metadata.GoTomatoVersion)
|
|
|
|
helper.Logger.Info("Websocket listening", "address", listen)
|
2024-10-30 06:37:14 +00:00
|
|
|
|
|
|
|
// start the listener
|
2024-11-03 09:00:14 +00:00
|
|
|
err := http.ListenAndServe(listen, handlers.ProxyHeaders(r))
|
2024-10-19 09:47:56 +00:00
|
|
|
if err != nil {
|
2024-11-04 19:33:25 +00:00
|
|
|
helper.Logger.Fatal("Error starting server:", "msg", err)
|
2024-10-19 09:47:56 +00:00
|
|
|
}
|
|
|
|
}
|