Skip to content

Commit

Permalink
Extract matchers into pkg/
Browse files Browse the repository at this point in the history
  • Loading branch information
spy16 committed Feb 1, 2019
1 parent 36212be commit e7aba86
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
11 changes: 8 additions & 3 deletions server/matcher.go → pkg/matcher/matcher.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server
package matcher

import (
"bytes"
Expand All @@ -14,9 +14,14 @@ import (
"github.com/savaki/jq"
)

type matcherFunc func(request *http.Request, shardExpr string) (shardKey string, err error)
func New(matcherName string) (MatcherFunc, bool) {
mf, found := matcherMux[matcherName]
return mf, found
}

type MatcherFunc func(request *http.Request, shardExpr string) (shardKey string, err error)

var matcherMux = map[string]matcherFunc{
var matcherMux = map[string]MatcherFunc{
"header": func(req *http.Request, expr string) (string, error) {
return req.Header.Get(expr), nil
},
Expand Down
2 changes: 1 addition & 1 deletion server/matcher_test.go → pkg/matcher/matcher_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server
package matcher

import (
"bytes"
Expand Down
7 changes: 4 additions & 3 deletions server/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"

"github.com/gojektech/weaver"
"github.com/gojektech/weaver/pkg/matcher"
"github.com/gojektech/weaver/pkg/shard"
"github.com/pkg/errors"
)
Expand All @@ -27,10 +28,8 @@ type EndpointConfig struct {
ShardConfig json.RawMessage `json:"shard_config"`
}

type shardKeyFunc func(*http.Request) (string, error)

func (endpointConfig *EndpointConfig) genShardKeyFunc() (shardKeyFunc, error) {
matcherFunc, found := matcherMux[endpointConfig.Matcher]
matcherFunc, found := matcher.New(endpointConfig.Matcher)
if !found {
return nil, errors.WithStack(fmt.Errorf("failed to find a matcherMux for: %s", endpointConfig.Matcher))
}
Expand Down Expand Up @@ -77,3 +76,5 @@ func (endpoint *Endpoint) Shard(request *http.Request) (*weaver.Backend, error)

return endpoint.sharder.Shard(shardKey)
}

type shardKeyFunc func(*http.Request) (string, error)

0 comments on commit e7aba86

Please sign in to comment.