Skip to content

Commit

Permalink
Merge pull request #695 from luraproject/listen_address
Browse files Browse the repository at this point in the history
Allow to configure the listen address of the service
  • Loading branch information
kpacha authored Oct 26, 2023
2 parents 8fc9527 + 16d335c commit 20a22f6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
15 changes: 15 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"net/textproto"
"regexp"
Expand Down Expand Up @@ -56,6 +57,8 @@ type ServiceConfig struct {
Host []string `mapstructure:"host"`
// port to bind the lura service
Port int `mapstructure:"port"`
// address to listen
Address string `mapstructure:"listen_ip"`
// version code of the configuration
Version int `mapstructure:"version"`
// OutputEncoding defines the default encoding strategy to use for the endpoint responses
Expand Down Expand Up @@ -404,6 +407,13 @@ func (s *ServiceConfig) initGlobalParams() error {
if s.Port == 0 {
s.Port = defaultPort
}

if s.Address != "" {
if !validateAddress(s.Address) {
return fmt.Errorf("invalid ip address %s", s.Address)
}
}

if s.MaxIdleConnsPerHost == 0 {
s.MaxIdleConnsPerHost = DefaultMaxIdleConnsPerHost
}
Expand Down Expand Up @@ -766,3 +776,8 @@ func SetSequentialParamsPattern(pattern string) error {
sequentialParamsPattern = re
return nil
}

func validateAddress(address string) bool {
ip := net.ParseIP(address)
return ip != nil
}
2 changes: 1 addition & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func TestConfig_init(t *testing.T) {
t.Error(err.Error())
}

if hash != "v28MFBnMvvy1JAQZcC3ZBhusgtxl/o0k+7R1NiK0M34=" {
if hash != "FK9W9HSD9jkExuf53M9SUs5bCwlwRde5kRX0cn8rGm4=" {
t.Errorf("unexpected hash: %s", hash)
}
}
Expand Down
2 changes: 2 additions & 0 deletions config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type parseableServiceConfig struct {
CacheTTL string `json:"cache_ttl"`
Host []string `json:"host"`
Port int `json:"port"`
Address string `json:"listen_ip"`
Version int `json:"version"`
ExtraConfig *ExtraConfig `json:"extra_config,omitempty"`
ReadTimeout string `json:"read_timeout"`
Expand Down Expand Up @@ -171,6 +172,7 @@ func (p *parseableServiceConfig) normalize() ServiceConfig {
CacheTTL: parseDuration(p.CacheTTL),
Host: p.Host,
Port: p.Port,
Address: p.Address,
Version: p.Version,
Debug: p.Debug,
Echo: p.Echo,
Expand Down
2 changes: 1 addition & 1 deletion transport/http/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func NewServer(cfg config.ServiceConfig, handler http.Handler) *http.Server {

func NewServerWithLogger(cfg config.ServiceConfig, handler http.Handler, logger logging.Logger) *http.Server {
return &http.Server{
Addr: fmt.Sprintf(":%d", cfg.Port),
Addr: net.JoinHostPort(cfg.Address, fmt.Sprintf("%d", cfg.Port)),
Handler: handler,
ReadTimeout: cfg.ReadTimeout,
WriteTimeout: cfg.WriteTimeout,
Expand Down

0 comments on commit 20a22f6

Please sign in to comment.