Skip to content

Commit

Permalink
improve support for hosted domains (#351)
Browse files Browse the repository at this point in the history
* improve support for hosted domains

* update travis lint config
  • Loading branch information
mthenw committed Nov 29, 2017
1 parent c5b413d commit 43a1d00
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ install:
script:
- go get -u github.com/alecthomas/gometalinter
- gometalinter --install --force
- gometalinter --vendor --fast --disable=gotype --disable=vetshadow --skip=mock ./...
- gometalinter --vendor --fast --disable=gotype --disable=vetshadow --disable=gas --skip=mock ./...
- go get github.com/mattn/goveralls
- goveralls -race -service=travis-ci -ignore=*/mock/*,*/*/mock/*
after_success:
Expand Down
3 changes: 2 additions & 1 deletion router/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"testing"
"time"

"go.uber.org/zap"

"github.com/julienschmidt/httprouter"
eventpkg "github.com/serverless/event-gateway/event"
"github.com/serverless/event-gateway/functions"
Expand All @@ -27,7 +29,6 @@ import (
"github.com/serverless/libkv/store"
etcd "github.com/serverless/libkv/store/etcd/v3"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)

func TestMain(t *testing.T) {
Expand Down
11 changes: 8 additions & 3 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (router *Router) WaitForSubscriber(path string, eventType eventpkg.Type) <-

// headerFunctionID is a header name for specifying function id for sync invocation.
const headerFunctionID = "function-id"
const hostedDomain = "eventgateway([a-z-]*)?.io"
const hostedDomain = "(eventgateway([a-z-]*)?.io|slsgateway.com)"

var (
errUnableToLookUpRegisteredFunction = errors.New("unable to look up registered function")
Expand Down Expand Up @@ -249,7 +249,6 @@ func (router *Router) handleHTTPEvent(event *eventpkg.Event, w http.ResponseWrit
if r.Method == http.MethodOptions && r.Header.Get("Access-Control-Request-Method") != "" {
reqMethod = r.Header.Get("Access-Control-Request-Method")
}

backingFunction, params, corsConfig := router.targetCache.HTTPBackingFunction(
strings.ToUpper(reqMethod), extractPath(r.Host, r.URL.EscapedPath()),
)
Expand Down Expand Up @@ -498,7 +497,13 @@ func extractPath(host, path string) string {
extracted := path
rxp, _ := regexp.Compile(hostedDomain)
if rxp.MatchString(host) {
extracted = "/" + strings.Split(host, ".")[0] + extracted
subdomain := strings.Split(host, ".")[0]
segments := strings.Split(subdomain, "---")
extracted = ""
for i := len(segments) - 1; i >= 0; i-- {
extracted += "/" + segments[i]
}
extracted += path
}
return extracted
}
Expand Down
15 changes: 15 additions & 0 deletions router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,21 @@ func TestRouterServeHTTP_AllowCORSPreflightForCustomEvents(t *testing.T) {
assert.Equal(t, "http://example.com", recorder.Header().Get("Access-Control-Allow-Origin"))
}

func TestRouterServeHTTP_ExtractPathFromHostedDomain(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
target := mock.NewMockTargeter(ctrl)
target.EXPECT().HTTPBackingFunction(http.MethodGet, "/name/app/dev/test").Return(nil, pathtree.Params{}, &cors.CORS{}).MaxTimes(1)
target.EXPECT().SubscribersOfEvent("/", event.SystemEventReceivedType).Return([]functions.FunctionID{}).MaxTimes(1)
router := testrouter(target)

req, _ := http.NewRequest(http.MethodGet, "https://dev---app---name.slsgateway.com/test", nil)
recorder := httptest.NewRecorder()
router.ServeHTTP(recorder, req)

assert.Equal(t, http.StatusNotFound, recorder.Code)
}

func testrouter(target Targeter) *Router {
log := zap.NewNop()
plugins := plugin.NewManager([]string{}, log)
Expand Down

0 comments on commit 43a1d00

Please sign in to comment.