Skip to content

Commit

Permalink
Allow user to specify HTTP and gRPC bind addresses (not just ports.)
Browse files Browse the repository at this point in the history
This allows us to use 'localhost' in tests, and prevents an 'Allow connections from...' dialog on MacOS when running unit tests.

Signed-off-by: Tom Wilkie <tom.wilkie@gmail.com>
  • Loading branch information
tomwilkie authored and ThoreKr committed Sep 26, 2019
1 parent a2b2a63 commit 9e413d8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import (
// Config for a Server
type Config struct {
MetricsNamespace string `yaml:"-"`
HTTPListenHost string `yaml:"http_listen_host"`
HTTPListenPort int `yaml:"http_listen_port"`
GRPCListenHost string `yaml:"grpc_listen_host"`
GRPCListenPort int `yaml:"grpc_listen_port"`

RegisterInstrumentation bool `yaml:"register_instrumentation"`
Expand Down Expand Up @@ -56,7 +58,9 @@ type Config struct {

// RegisterFlags adds the flags required to config this to the given FlagSet
func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
f.StringVar(&cfg.HTTPListenHost, "server.http-listen-host", "", "HTTP server listen host.")
f.IntVar(&cfg.HTTPListenPort, "server.http-listen-port", 80, "HTTP server listen port.")
f.StringVar(&cfg.GRPCListenHost, "server.grpc-listen-host", "", "gRPC server listen host.")
f.IntVar(&cfg.GRPCListenPort, "server.grpc-listen-port", 9095, "gRPC server listen port.")
f.BoolVar(&cfg.RegisterInstrumentation, "server.register-instrumentation", true, "Register the intrumentation handlers (/metrics etc).")
f.DurationVar(&cfg.ServerGracefulShutdownTimeout, "server.graceful-shutdown-timeout", 30*time.Second, "Timeout for graceful shutdowns")
Expand Down Expand Up @@ -88,12 +92,12 @@ type Server struct {
// New makes a new Server
func New(cfg Config) (*Server, error) {
// Setup listeners first, so we can fail early if the port is in use.
httpListener, err := net.Listen("tcp", fmt.Sprintf(":%d", cfg.HTTPListenPort))
httpListener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.HTTPListenHost, cfg.HTTPListenPort))
if err != nil {
return nil, err
}

grpcListener, err := net.Listen("tcp", fmt.Sprintf(":%d", cfg.GRPCListenPort))
grpcListener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.GRPCListenHost, cfg.GRPCListenPort))
if err != nil {
return nil, err
}
Expand Down
7 changes: 7 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func (f FakeServer) Succeed(ctx context.Context, req *google_protobuf.Empty) (*g
func TestErrorInstrumentationMiddleware(t *testing.T) {
var cfg Config
cfg.RegisterFlags(flag.NewFlagSet("", flag.ExitOnError))
cfg.HTTPListenHost = "localhost"
cfg.HTTPListenPort = 9190
cfg.GRPCListenHost = "localhost"
cfg.GRPCListenPort = 1234
server, err := New(cfg)
require.NoError(t, err)
Expand Down Expand Up @@ -102,7 +105,9 @@ func TestErrorInstrumentationMiddleware(t *testing.T) {

func TestRunReturnsError(t *testing.T) {
cfg := Config{
HTTPListenHost: "localhost",
HTTPListenPort: 9190,
GRPCListenHost: "localhost",
GRPCListenPort: 9191,
}
t.Run("http", func(t *testing.T) {
Expand Down Expand Up @@ -142,7 +147,9 @@ func TestMiddlewareLogging(t *testing.T) {
var level logging.Level
level.Set("info")
cfg := Config{
HTTPListenHost: "localhost",
HTTPListenPort: 9192,
GRPCListenHost: "localhost",
HTTPMiddleware: []middleware.Interface{middleware.Logging},
MetricsNamespace: "testing_logging",
LogLevel: level,
Expand Down

0 comments on commit 9e413d8

Please sign in to comment.