From d2ae0896b5350d48eb6d201870700cc05ecd7d81 Mon Sep 17 00:00:00 2001 From: Michael Nikitochkin Date: Thu, 15 Sep 2022 21:31:48 +0200 Subject: [PATCH] Update interface of ApiServer.Listen to use addr. Provide addr instead of host and port as separate parameters. Old: ```go server.Listen("localhost", "8474") ``` New: ```go server.Listen("localhost:8474") ``` --- CHANGELOG.md | 1 + api.go | 4 +--- api_test.go | 2 +- cmd/server/server.go | 8 +++++--- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc836266..a0ca9872 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * Client specifies `User-Agent` HTTP header for all requests as "toxiproxy-cli/ /". Specifies client request content type as `application/json`. (#441, @miry) +* Replace Api.Listen parameters `host` and `port` with single `addr`. (#445, @miry) # [2.5.0] - 2022-09-10 diff --git a/api.go b/api.go index b7c5ef4c..d639749f 100644 --- a/api.go +++ b/api.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "net" "net/http" "os" "strings" @@ -51,8 +50,7 @@ func NewServer(m *metricsContainer, logger zerolog.Logger) *ApiServer { } } -func (server *ApiServer) Listen(host string, port string) error { - addr := net.JoinHostPort(host, port) +func (server *ApiServer) Listen(addr string) error { server.Logger. Info(). Str("address", addr). diff --git a/api_test.go b/api_test.go index aeee36e2..322cfe07 100644 --- a/api_test.go +++ b/api_test.go @@ -34,7 +34,7 @@ func WithServer(t *testing.T, f func(string)) { log, ) - go testServer.Listen("localhost", "8475") + go testServer.Listen("localhost:8475") // Allow server to start. There's no clean way to know when it listens. time.Sleep(50 * time.Millisecond) diff --git a/cmd/server/server.go b/cmd/server/server.go index 465fb7f7..d188adb1 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "math/rand" + "net" "os" "os/signal" "strconv" @@ -89,12 +90,13 @@ func run() error { server.PopulateConfig(cli.config) } - go func(server *toxiproxy.ApiServer, host, port string) { - err := server.Listen(host, port) + addr := net.JoinHostPort(cli.host, cli.port) + go func(server *toxiproxy.ApiServer, addr string) { + err := server.Listen(addr) if err != nil { server.Logger.Err(err).Msg("Server finished with error") } - }(server, cli.host, cli.port) + }(server, addr) signals := make(chan os.Signal, 1) signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)