From 0b2f83cd35bbc73fcedd5944277297c351a835db Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sun, 20 Oct 2024 14:55:02 +0200 Subject: [PATCH] feat: add CLI flags to app MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - implement command-line flags for listen address and port - add usage section to README 🤖 --- README.md | 4 ++++ cmd/server/main.go | 16 +++++++++++----- pkg/models/config.go | 4 ++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2769187..0362d80 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,10 @@ A pomodoro server written in Go `go install git.smsvc.net/pomodoro/GoTomato@latest` +## Usage + +See `GoTomato --help` for Parameters + ## Testing ``` diff --git a/cmd/server/main.go b/cmd/server/main.go index 3d1c9f8..2105b39 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,6 +1,7 @@ package server import ( + "flag" "fmt" "git.smsvc.net/pomodoro/GoTomato/internal/websocket" "git.smsvc.net/pomodoro/GoTomato/pkg/models" @@ -8,12 +9,17 @@ import ( "net/http" ) -var serverConfig = models.GoTomatoServerConfig{ - ListenAddress: "0.0.0.0", - ListenPort: 8080, -} - func Start() { + // 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, + } + listen := fmt.Sprintf("%s:%d", serverConfig.ListenAddress, serverConfig.ListenPort) http.HandleFunc("/ws", websocket.HandleConnections) diff --git a/pkg/models/config.go b/pkg/models/config.go index 628e685..33a79d4 100644 --- a/pkg/models/config.go +++ b/pkg/models/config.go @@ -8,6 +8,6 @@ type GoTomatoTimerConfig struct { } type GoTomatoServerConfig struct { - ListenAddress string `json:"listenAddress"` - ListenPort int `json:"listenPort"` + ListenAddress string `json:"listenAddress, default:"0.0.0.0"` + ListenPort int `json:"listenPort, default:8080"` }