Skip to content

Commit

Permalink
Fix multi hosts routing configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Ludwig committed Nov 9, 2020
1 parent 51981b8 commit 771b30c
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 13 deletions.
40 changes: 34 additions & 6 deletions config/runtime/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func NewServerConfiguration(conf *config.Gateway, httpConf *HTTPConfig, log *log
}

if srvConf.API == nil {
if err = configureHandlers(defaultPort, srvConf, muxOptions, server); err != nil {
if err = mapPortRoutes(defaultPort, srvConf, muxOptions, server); err != nil {
return nil, err
}
continue
Expand Down Expand Up @@ -199,7 +199,7 @@ func NewServerConfiguration(conf *config.Gateway, httpConf *HTTPConfig, log *log
}
}

if err = configureHandlers(defaultPort, srvConf, muxOptions, server); err != nil {
if err = mapPortRoutes(defaultPort, srvConf, muxOptions, server); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -250,7 +250,7 @@ func newBackendsFromDefinitions(conf *config.Gateway, log *logrus.Entry) (map[st
return backends, nil
}

func configureHandlers(configuredPort int, server *config.Server, mux *MuxOptions, srvMux Server) error {
func mapPortRoutes(configuredPort int, server *config.Server, mux *MuxOptions, srvMux Server) error {
hosts := server.Hosts
if len(hosts) == 0 {
hosts = []string{fmt.Sprintf("*:%d", configuredPort)}
Expand All @@ -263,9 +263,37 @@ func configureHandlers(configuredPort int, server *config.Server, mux *MuxOption
if err != nil {
return err
}
port = Port(p)
if p != "*" {
port = Port(p)
}
}

if _, ok := srvMux[port]; !ok {
srvMux[port] = &ServerMux{Server: server, Mux: mux}
} else {
for i, routes := range []map[string]http.Handler{mux.EndpointRoutes, mux.FileRoutes, mux.SPARoutes} {
for route, handler := range routes {
idx := strings.IndexByte(route, '/')
if idx < 0 || (hp != "*" && !strings.HasSuffix(route[:idx], ":"+string(port))) {
continue
}

var routesMap map[string]http.Handler
switch i {
case 0:
routesMap = srvMux[port].Mux.EndpointRoutes
case 1:
routesMap = srvMux[port].Mux.FileRoutes
case 2:
routesMap = srvMux[port].Mux.SPARoutes
}
if _, ok := routesMap[route]; ok {
return fmt.Errorf("duplicate route on port: %v: %q", port, route)
}
routesMap[route] = handler
}
}
}
srvMux[port] = &ServerMux{Server: server, Mux: mux}
}
return nil
}
Expand Down Expand Up @@ -413,7 +441,7 @@ func getPathsFromHosts(defaultPort int, hosts []string, path string) []string {
list = append(list, utils.JoinPath(pathpattern.PathFromHost(host, false), "/", path))
}
if len(list) == 0 {
list = []string{path}
list = []string{utils.JoinPath("/", path)}
}
return list
}
Expand Down
36 changes: 29 additions & 7 deletions server/http_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestMain(m *testing.M) {
}

func setup() {
println("create test backend...")
println("INTEGRATION: create test backend...")
testBackend = test.NewBackend()

wd, err := os.Getwd()
Expand All @@ -47,7 +47,7 @@ func setup() {
}

func teardown() {
println("close test backend...")
println("INTEGRATION: close test backend...")
testBackend.Close()
}

Expand Down Expand Up @@ -141,6 +141,28 @@ func TestHTTPServer_ServeHTTP(t *testing.T) {
expectation{http.StatusOK, []byte(`<html lang="en">index</html>`), nil, "file"},
},
}},
{"files/02_couper.hcl", []requestCase{
{
testRequest{http.MethodGet, "http://anyserver:8080/a"},
expectation{http.StatusOK, []byte(`<html lang="en">index A</html>`), nil, "file"},
},
{
testRequest{http.MethodGet, "http://couper.io:9898/a"},
expectation{http.StatusOK, []byte(`<html lang="en">index A</html>`), nil, "file"},
},
{
testRequest{http.MethodGet, "http://couper.io:9898/"},
expectation{http.StatusInternalServerError, []byte("<html>1002</html>"), nil, ""},
},
{
testRequest{http.MethodGet, "http://example.com:9898/b"},
expectation{http.StatusOK, []byte(`<html lang="en">index B</html>`), nil, "file"},
},
{
testRequest{http.MethodGet, "http://example.com:9898/"},
expectation{http.StatusInternalServerError, []byte("<html>1002</html>"), nil, ""},
},
}},
{"files_spa_api/01_couper.hcl", []requestCase{
{
testRequest{http.MethodGet, "http://anyserver:8080/"},
Expand Down Expand Up @@ -202,12 +224,12 @@ func TestHTTPServer_ServeHTTP(t *testing.T) {
},
{
testRequest{http.MethodGet, "http://couper.io:9898/v2/not-found"},
expectation{http.StatusBadGateway, []byte(`{"code": 4001}`), http.Header{"Content-Type": {"application/json"}}, ""},
},
{
testRequest{http.MethodGet, "http://example.com:9898/v3/not-found"},
expectation{http.StatusBadGateway, []byte(`{"code": 4001}`), http.Header{"Content-Type": {"application/json"}}, ""},
expectation{http.StatusNotFound, []byte(`{"code": 4001}`), http.Header{"Content-Type": {"application/json"}}, ""},
},
//{ // TODO: Fix multi-api server/host behaviour - this would lead to multi apiBasePaths
// testRequest{http.MethodGet, "http://example.com:9898/v3/not-found"},
// expectation{http.StatusNotFound, []byte(`{"code": 4001}`), http.Header{"Content-Type": {"application/json"}}, ""},
//},
}},
} {
confPath := path.Join("testdata/integration", testcase.fileName)
Expand Down
19 changes: 19 additions & 0 deletions server/testdata/integration/files/02_couper.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
server "multi-files-host1" {
hosts = ["*", "couper.io:9898"]
error_file = "./../server_error.html"

files {
base_path = "/a"
document_root = "./htdocs_a"
}
}

server "multi-files-host2" {
hosts = ["example.com:9898"]
error_file = "./../server_error.html"

base_path = "/b"
files {
document_root = "./htdocs_b"
}
}
1 change: 1 addition & 0 deletions server/testdata/integration/files/htdocs_a/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html lang="en">index A</html>
1 change: 1 addition & 0 deletions server/testdata/integration/files/htdocs_b/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html lang="en">index B</html>

0 comments on commit 771b30c

Please sign in to comment.