GoTomato/cmd/server/main.go

67 lines
1.9 KiB
Go
Raw Normal View History

package server
import (
"flag"
"fmt"
"github.com/gorilla/handlers"
"net/http"
"os"
"git.smsvc.net/pomodoro/GoTomato/internal/helper"
"git.smsvc.net/pomodoro/GoTomato/internal/metadata"
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
"git.smsvc.net/pomodoro/GoTomato/internal/websocket"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
var (
2024-10-30 06:37:14 +00:00
// define CLI flags
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-30 06:37:14 +00:00
// Start the pomodoro server
func Start() {
// Update usage output
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", "GoTomato")
flag.PrintDefaults()
fmt.Println("\nPoint your client to 'ws://<listenAddress>:<listenPort>'")
}
flag.Parse()
2024-10-30 06:37:14 +00:00
// show server and protocl version and exit
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)
os.Exit(0)
}
2024-10-30 06:37:14 +00:00
// set server config
serverConfig := models.ServerConfig{
ListenAddress: *listenAddress,
ListenPort: *listenPort,
}
shared.PomodoroPassword = *password
2024-10-30 06:37:14 +00:00
// define listen address for websocket
listen := fmt.Sprintf("%s:%d", serverConfig.ListenAddress, serverConfig.ListenPort)
2024-10-30 06:37:14 +00:00
// start connection handler and broadcast
r := http.NewServeMux()
r.HandleFunc("/", websocket.HandleConnection)
go websocket.SendPermanentBroadCastMessage()
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
err := http.ListenAndServe(listen, handlers.ProxyHeaders(r))
if err != nil {
helper.Logger.Fatal("Error starting server:", "msg", err)
}
}