Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
reinkrul committed Feb 11, 2021
1 parent 3c60fda commit 2fcc614
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func startServer(system *core.System) error {
}

// init HTTP interfaces and routes
echoServer := core.NewMultiEcho(system.EchoCreator, system.Config.HTTP.HttpConfig)
echoServer := core.NewMultiEcho(system.EchoCreator, system.Config.HTTP.HTTPConfig)
for httpGroup, httpConfig := range system.Config.HTTP.AltBinds {
logrus.Infof("Binding /%s -> %s", httpGroup, httpConfig.Address)
if err := echoServer.Bind(httpGroup, httpConfig); err != nil {
Expand Down
27 changes: 26 additions & 1 deletion cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"bytes"
"errors"
"github.com/labstack/echo/v4"
"os"
"testing"
Expand Down Expand Up @@ -80,7 +81,7 @@ func Test_serverCmd(t *testing.T) {
}
system.Config = core.NewServerConfig()
system.Config.Datadir = io.TestDirectory(t)
system.Config.HTTP.AltBinds["internal"] = core.HttpConfig{Address: "localhost:7642"}
system.Config.HTTP.AltBinds["internal"] = core.HTTPConfig{Address: "localhost:7642"}
err := startServer(system)
if !assert.NoError(t, err) {
return
Expand All @@ -89,6 +90,30 @@ func Test_serverCmd(t *testing.T) {
assert.Equal(t, system.Config.HTTP.Address, echoServers[0].address)
assert.Equal(t, "localhost:7642", echoServers[1].address)
})
t.Run("unable to configure system", func(t *testing.T) {
system := core.NewSystem()
system.Config = core.NewServerConfig()
system.Config.Datadir = "root_test.go"
err := startServer(system)
assert.Error(t, err, "unable to start")
})
t.Run("alt binds error", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

echoServer := core.NewMockEchoServer(ctrl)
echoServer.EXPECT().Start(gomock.Any()).Return(errors.New("unable to start"))

system := core.NewSystem()
system.EchoCreator = func() core.EchoServer {
return echoServer
}
system.Config = core.NewServerConfig()
system.Config.Datadir = io.TestDirectory(t)
system.Config.HTTP.AltBinds["internal"] = core.HTTPConfig{Address: "localhost:7642"}
err := startServer(system)
assert.EqualError(t, err, "unable to start")
})
}

func Test_CreateSystem(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions core/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const defaultEchoGroup = ""

// NewMultiEcho creates a new MultiEcho which uses the given function to create EchoServers. If a route is registered
// for an unknown group is is bound to the given defaultInterface.
func NewMultiEcho(creatorFn func() EchoServer, defaultInterface HttpConfig) *MultiEcho {
func NewMultiEcho(creatorFn func() EchoServer, defaultInterface HTTPConfig) *MultiEcho {
instance := &MultiEcho{
interfaces: map[string]EchoServer{},
groups: map[string]string{},
Expand All @@ -48,7 +48,7 @@ type MultiEcho struct {

// Bind binds the given group (first part of the URL) to the given HTTP interface. Calling Bind for the same group twice
// results in an error being returned.
func (c *MultiEcho) Bind(group string, interfaceConfig HttpConfig) error {
func (c *MultiEcho) Bind(group string, interfaceConfig HTTPConfig) error {
normGroup := strings.ToLower(group)
if _, groupExists := c.groups[normGroup]; groupExists {
return fmt.Errorf("http bind group already exists: %s", group)
Expand Down
12 changes: 6 additions & 6 deletions core/echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func Test_MultiEcho_Bind(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

cfg := NewServerConfig().HTTP.HttpConfig
cfg := NewServerConfig().HTTP.HTTPConfig
m := NewMultiEcho(func() EchoServer {
return NewMockEchoServer(ctrl)
}, cfg)
Expand All @@ -38,7 +38,7 @@ func Test_MultiEcho_Routes(t *testing.T) {

m := NewMultiEcho(func() EchoServer {
return s
}, NewServerConfig().HTTP.HttpConfig)
}, NewServerConfig().HTTP.HTTPConfig)
m.PATCH("patch", nil)
m.GET("get", nil)
m.OPTIONS("options", nil)
Expand All @@ -56,7 +56,7 @@ func Test_MultiEcho(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

defaultHttpCfg := NewServerConfig().HTTP.HttpConfig
defaultHttpCfg := NewServerConfig().HTTP.HTTPConfig

// Set up expected echo servers
defaultServer := NewMockEchoServer(ctrl)
Expand All @@ -82,15 +82,15 @@ func Test_MultiEcho(t *testing.T) {

// Bind interfaces
m := NewMultiEcho(createFn, defaultHttpCfg)
err := m.Bind("internal", HttpConfig{Address: "internal:8080"})
err := m.Bind("internal", HTTPConfig{Address: "internal:8080"})
if !assert.NoError(t, err) {
return
}
err = m.Bind("public", HttpConfig{Address: "public:8080"})
err = m.Bind("public", HTTPConfig{Address: "public:8080"})
if !assert.NoError(t, err) {
return
}
err = m.Bind("extra-public", HttpConfig{Address: "public:8080"})
err = m.Bind("extra-public", HTTPConfig{Address: "public:8080"})
if !assert.NoError(t, err) {
return
}
Expand Down
22 changes: 11 additions & 11 deletions core/server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,22 @@ type ServerConfig struct {
Verbosity string `koanf:"verbosity"`
Strictmode bool `koanf:"strictmode"`
Datadir string `koanf:"datadir"`
HTTP GlobalHttpConfig `koanf:"http"`
HTTP GlobalHTTPConfig `koanf:"http"`
configMap *koanf.Koanf
}

// GlobalHttpConfig is the top-level config struct for HTTP interfaces.
type GlobalHttpConfig struct {
// HttpConfig contains the config for the default HTTP interface.
HttpConfig `koanf:"default"`
// GlobalHTTPConfig is the top-level config struct for HTTP interfaces.
type GlobalHTTPConfig struct {
// HTTPConfig contains the config for the default HTTP interface.
HTTPConfig `koanf:"default"`
// AltBinds contains binds for alternative HTTP interfaces. The key of the map is the first part of the path
// of the URL (e.g. `/internal/some-api` -> `internal`), the value is the HTTP interface it must be bound to.
AltBinds map[string]HttpConfig `koanf:"alt"`
AltBinds map[string]HTTPConfig `koanf:"alt"`
}

// HttpConfig contains configuration for an HTTP interface, e.g. address.
// HTTPConfig contains configuration for an HTTP interface, e.g. address.
// It will probably contain security related properties in the future (TLS configuration, user/pwd requirements).
type HttpConfig struct {
type HTTPConfig struct {
// Address holds the interface address the HTTP service must be bound to, in the format of `host:port` (e.g. localhost:5555).
Address string `koanf:"address"`
}
Expand All @@ -77,9 +77,9 @@ func NewServerConfig() *ServerConfig {
Verbosity: defaultLogLevel,
Strictmode: defaultStrictMode,
Datadir: defaultDatadir,
HTTP: GlobalHttpConfig{
HttpConfig: HttpConfig{Address: defaultAddress},
AltBinds: map[string]HttpConfig{},
HTTP: GlobalHTTPConfig{
HTTPConfig: HTTPConfig{Address: defaultAddress},
AltBinds: map[string]HTTPConfig{},
},
}
}
Expand Down

0 comments on commit 2fcc614

Please sign in to comment.