Skip to content

Commit

Permalink
Merge 439ae07 into f8b1cbc
Browse files Browse the repository at this point in the history
  • Loading branch information
kpacha committed Apr 7, 2019
2 parents f8b1cbc + 439ae07 commit 6b1d29b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 36 deletions.
101 changes: 67 additions & 34 deletions config/config.go
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -290,9 +289,20 @@ func (s *ServiceConfig) Hash() (string, error) {
// normalizes all the things.
func (s *ServiceConfig) Init() error {
s.uriParser = NewURIParser()

if s.Version != ConfigVersion {
return &UnsupportedVersionError{Have: s.Version, Want: ConfigVersion}
return &UnsupportedVersionError{
Have: s.Version,
Want: ConfigVersion,
}
}

s.initGlobalParams()

return s.initEndpoints()
}

func (s *ServiceConfig) initGlobalParams() {
if s.Port == 0 {
s.Port = defaultPort
}
Expand All @@ -306,7 +316,9 @@ func (s *ServiceConfig) Init() error {
s.Host = s.uriParser.CleanHosts(s.Host)

s.ExtraConfig.sanitize()
}

func (s *ServiceConfig) initEndpoints() error {
for i, e := range s.Endpoints {
e.Endpoint = s.uriParser.CleanPath(e.Endpoint)

Expand Down Expand Up @@ -340,7 +352,6 @@ func (s *ServiceConfig) Init() error {
b.ExtraConfig.sanitize()
}
}

return nil
}

Expand Down Expand Up @@ -404,45 +415,22 @@ func (s *ServiceConfig) initBackendURLMappings(e, b int, inputParams map[string]

backend.URLPattern = s.uriParser.CleanPath(backend.URLPattern)

outputParams := s.extractPlaceHoldersFromURLTemplate(backend.URLPattern, simpleURLKeysPattern)

outputSet := map[string]interface{}{}
outputSetSize := 0
for _, op := range outputParams {
if _, ok := outputSet[op]; ok {
continue
}
outputSet[op] = nil
if sequentialParamsPattern.MatchString(op) {
continue
}
outputSetSize++
}

ip := []string{}
for i := range inputParams {
ip = append(ip, i)
}
sort.Strings(ip)
outputParams, outputSetSize := uniqueOutput(s.extractPlaceHoldersFromURLTemplate(backend.URLPattern, simpleURLKeysPattern))

op := []string{}
for o := range outputSet {
op = append(op, o)
}
sort.Strings(op)
ip := fromSetToSortedSlice(inputParams)

if outputSetSize > len(inputParams) {
if outputSetSize > len(ip) {
return &WrongNumberOfParamsError{
Endpoint: s.Endpoints[e].Endpoint,
Method: s.Endpoints[e].Method,
Backend: b,
InputParams: ip,
OutputParams: op,
OutputParams: outputParams,
}
}

backend.URLKeys = []string{}
for _, output := range op {
for _, output := range outputParams {
if !sequentialParamsPattern.MatchString(output) {
if _, ok := inputParams[output]; !ok {
return &UndefinedOutputParamError{
Expand All @@ -451,7 +439,7 @@ func (s *ServiceConfig) initBackendURLMappings(e, b int, inputParams map[string]
Method: s.Endpoints[e].Method,
Backend: b,
InputParams: ip,
OutputParams: op,
OutputParams: outputParams,
}
}
}
Expand All @@ -462,11 +450,43 @@ func (s *ServiceConfig) initBackendURLMappings(e, b int, inputParams map[string]
return nil
}

func fromSetToSortedSlice(set map[string]interface{}) []string {
res := []string{}
for element := range set {
res = append(res, element)
}
sort.Strings(res)
return res
}

func uniqueOutput(output []string) ([]string, int) {
sort.Strings(output)
j := 0
outputSetSize := 0
for i := 1; i < len(output); i++ {
if output[j] == output[i] {
continue
}
if !sequentialParamsPattern.MatchString(output[j]) {
outputSetSize++
}
j++
output[j] = output[i]
}
if j == len(output) {
return output, outputSetSize
}
return output[:j+1], outputSetSize
}

func (e *EndpointConfig) validate() error {
matched, err := regexp.MatchString(debugPattern, e.Endpoint)
if err != nil {
log.Printf("ERROR: parsing the endpoint url '%s': %s. Ignoring\n", e.Endpoint, err.Error())
return err
return &EndpointMatchError{
Err: err,
Path: e.Endpoint,
Method: e.Method,
}
}
if matched {
return &EndpointPathError{Path: e.Endpoint, Method: e.Method}
Expand All @@ -478,6 +498,19 @@ func (e *EndpointConfig) validate() error {
return nil
}

// EndpointMatchError is the error returned by the configuration init process when the endpoint pattern
// check fails
type EndpointMatchError struct {
Path string
Method string
Err error
}

// Error returns a string representation of the EndpointMatchError
func (e *EndpointMatchError) Error() string {
return fmt.Sprintf("ERROR: parsing the endpoint url '%s %s': %s. Ignoring", e.Method, e.Path, e.Err.Error())
}

// NoBackendsError is the error returned by the configuration init process when an endpoint
// is connected to 0 backends
type NoBackendsError struct {
Expand Down
4 changes: 2 additions & 2 deletions config/config_test.go
Expand Up @@ -285,14 +285,14 @@ func TestConfig_initKOInvalidDebugPattern(t *testing.T) {
Endpoints: []*EndpointConfig{
{
Endpoint: "/__debug/supu",
Method: "get",
Method: "GET",
Backend: []*Backend{},
},
},
}

if err := subject.Init(); err == nil ||
err.Error() != "error parsing regexp: missing closing ): `a(b`" {
err.Error() != "ERROR: parsing the endpoint url 'GET /__debug/supu': error parsing regexp: missing closing ): `a(b`. Ignoring" {
t.Error("Expecting an error at the configuration init!", err)
}

Expand Down

0 comments on commit 6b1d29b

Please sign in to comment.