GoTomato/cmd/server/main.go
Sebastian Mark 44a64bfce4 feat: handle X-Forward-For for log output
- add Gorilla handlers package for enhanced HTTP handling
- refactor HTTP server to use a new ServeMux for routing
- update ListenAndServe to utilize ProxyHeaders for better proxy support
- add RealIP field to client model
  - use RealIP fild for connect/disconnect log output
2024-11-03 11:17:23 +01:00

59 lines
1.7 KiB
Go

package server
import (
"flag"
"fmt"
"github.com/charmbracelet/log"
"github.com/gorilla/handlers"
"net/http"
"os"
"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 (
// 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)")
showVersionFlag = flag.Bool("version", false, "Output version")
)
// Start the pomodoro server
func Start() {
flag.Parse()
// show server and protocl version and exit
if *showVersionFlag {
fmt.Println("Server-Version:", metadata.GoTomatoVersion)
fmt.Println("Protocol-Version:", metadata.ProtocolVersion)
os.Exit(0)
}
// set server config
serverConfig := models.ServerConfig{
ListenAddress: *listenAddress,
ListenPort: *listenPort,
}
shared.PomodoroPassword = *password
// define listen address for websocket
listen := fmt.Sprintf("%s:%d", serverConfig.ListenAddress, serverConfig.ListenPort)
// start connection handler and broadcast
r := http.NewServeMux()
r.HandleFunc("/ws", websocket.HandleConnection)
go websocket.SendPermanentBroadCastMessage()
log.Info("GoTomato started", "version", metadata.GoTomatoVersion)
log.Info("Websocket listening", "address", listen)
// start the listener
err := http.ListenAndServe(listen, handlers.ProxyHeaders(r))
if err != nil {
log.Fatal("Error starting server:", "msg", err)
}
}