Skip to content

Commit

Permalink
Update interface of ApiServer.Listen to use addr.
Browse files Browse the repository at this point in the history
Provide addr instead of host and port as separate parameters.

Old:
```go
server.Listen("localhost", "8474")
```

New:
```go
server.Listen("localhost:8474")
```
  • Loading branch information
miry committed Sep 15, 2022
1 parent 48d5c8a commit 0719fd0
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Client specifies `User-Agent` HTTP header for all requests as
"toxiproxy-cli/<version> <os>/<runtime>".
Specifies client request content type as `application/json`. (#441, @miry)
* Replace Api.Listen parameters `host` and `port` with single `addr`. (@miry)

# [2.5.0] - 2022-09-10

Expand Down
4 changes: 1 addition & 3 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"os"
"strings"
Expand Down Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"math/rand"
"net"
"os"
"os/signal"
"strconv"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 0719fd0

Please sign in to comment.