Skip to content

Commit

Permalink
Handle defaultPorts for pathsHosts #59
Browse files Browse the repository at this point in the history
Fix usage of settings.default_port
  • Loading branch information
Marcel Ludwig committed Oct 26, 2020
1 parent 75c0a46 commit 698c947
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
7 changes: 5 additions & 2 deletions config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ func LoadFile(filename string) (*Gateway, error) {
}
src, err := ioutil.ReadFile(path.Join(wd, filename))
if err != nil {
return nil, fmt.Errorf("Failed to load configuration: %w", err)
return nil, fmt.Errorf("failed to load configuration: %w", err)
}
return LoadBytes(src)
}

func LoadBytes(src []byte) (*Gateway, error) {
config := &Gateway{Context: eval.NewENVContext(src)}
config := &Gateway{
Context: eval.NewENVContext(src),
Settings: &Settings{DefaultPort: DefaultListenPort},
}
// filename must match .hcl ending for further []byte processing
if err := hclsimple.Decode("loadBytes.hcl", src, config.Context, config); err != nil {
return nil, fmt.Errorf("Failed to load configuration bytes: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion config/runtime/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var DefaultHTTP = &HTTPConfig{
ShutdownDelay: time.Second * 5,
ShutdownTimeout: time.Second * 5,
},
ListenPort: 8080,
ListenPort: config.DefaultListenPort,
RequestIDFormat: "common",
}

Expand Down
40 changes: 29 additions & 11 deletions config/runtime/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ func NewServerConfiguration(conf *config.Gateway, httpConf *HTTPConfig, log *log
log.Fatal("Missing server definitions")
}

// (arg && env) > conf
defaultPort := conf.Settings.DefaultPort
if httpConf.ListenPort != defaultPort {
defaultPort = httpConf.ListenPort
}

type backendDefinition struct {
conf *config.Backend
handler http.Handler
Expand Down Expand Up @@ -91,7 +97,7 @@ func NewServerConfiguration(conf *config.Gateway, httpConf *HTTPConfig, log *log
log.Fatal(err)
}
for _, spaPath := range srvConf.Spa.Paths {
for _, p := range getPathsFromHosts(srvConf.Hosts, spaPath) {
for _, p := range getPathsFromHosts(defaultPort, srvConf.Hosts, spaPath) {
muxOptions.SPARoutes[p] = spaHandler
}
}
Expand All @@ -110,7 +116,7 @@ func NewServerConfiguration(conf *config.Gateway, httpConf *HTTPConfig, log *log
}

if srvConf.API == nil {
if err = configureHandlers(httpConf, srvConf, muxOptions, server); err != nil {
if err = configureHandlers(defaultPort, srvConf, muxOptions, server); err != nil {
log.Fatal(err)
}
continue
Expand Down Expand Up @@ -221,29 +227,31 @@ func NewServerConfiguration(conf *config.Gateway, httpConf *HTTPConfig, log *log

setACHandlerFn(backendDefinition{conf: inlineConf, handler: inlineBackend})

for _, hostPath := range getPathsFromHosts(srvConf.Hosts, pattern) {
for _, hostPath := range getPathsFromHosts(defaultPort, srvConf.Hosts, pattern) {
muxOptions.EndpointRoutes[hostPath] = api[endpoint]
}
}

if err = configureHandlers(httpConf, srvConf, muxOptions, server); err != nil {
if err = configureHandlers(defaultPort, srvConf, muxOptions, server); err != nil {
log.Fatal(err)
}
}
return server
}

func configureHandlers(conf *HTTPConfig, server *config.Server, mux *MuxOptions, srvMux Server) error {
func configureHandlers(configuredPort int, server *config.Server, mux *MuxOptions, srvMux Server) error {
hosts := server.Hosts
if len(hosts) == 0 {
hosts = []string{fmt.Sprintf("*:%d", conf.ListenPort)}
hosts = []string{fmt.Sprintf("*:%d", configuredPort)}
}

for _, hp := range hosts {
port := Port(strconv.Itoa(conf.ListenPort))
if _, p, err := net.SplitHostPort(hp); err != nil {
return err
} else if p != "" {
port := Port(strconv.Itoa(configuredPort))
if strings.IndexByte(hp, ':') > 0 {
_, p, err := net.SplitHostPort(hp)
if err != nil {
return err
}
port = Port(p)
}
srvMux[port] = &ServerMux{Server: server, Mux: mux}
Expand Down Expand Up @@ -383,12 +391,22 @@ func newInlineBackend(evalCtx *hcl.EvalContext, inlineDef hcl.Body, cors *config
return proxy, beConf, err
}

func getPathsFromHosts(hosts []string, path string) []string {
func getPathsFromHosts(defaultPort int, hosts []string, path string) []string {
var list []string
port := strconv.Itoa(defaultPort)
for _, host := range hosts {
if host != "" && host[0] == ':' {
continue
}

if strings.IndexByte(host, ':') < 0 {
host = host + ":" + port
}

if host != "" && host[0] == '*' {
host = ""
}

list = append(list, utils.JoinPath(pathpattern.PathFromHost(host, false), "/", path))
}
if len(list) == 0 {
Expand Down
2 changes: 2 additions & 0 deletions config/settings.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package config

const DefaultListenPort = 8080

type Settings struct {
DefaultPort int `hcl:"default_port,optional"`
HealthPath string `hcl:"health_path,optional"`
Expand Down

0 comments on commit 698c947

Please sign in to comment.