Skip to content

Commit

Permalink
Clearned up output and error management
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelycode committed Sep 9, 2015
1 parent df9ca51 commit 235f127
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
54 changes: 30 additions & 24 deletions service_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *ServiceDiscovery) New(spec *APISpec) {
}

func (s *ServiceDiscovery) getServiceData(name string) (string, error) {
log.Info("Getting ", name)
log.Debug("Getting ", name)
resp, err := http.Get(s.spec.Proxy.ServiceDiscovery.QueryEndpoint)
if err != nil {
return "", err
Expand All @@ -62,16 +62,16 @@ func (s *ServiceDiscovery) decodeRawJsonString(value string) interface{} {
}

func (s *ServiceDiscovery) decodeToNameSpace(namespace string, jsonParsed *gabs.Container) interface{} {
log.Info("Namespace: ", namespace)
log.Debug("Namespace: ", namespace)
value := jsonParsed.Path(namespace).Data()
return value
}

func (s *ServiceDiscovery) decodeToNameSpaceAsArray(namespace string, jsonParsed *gabs.Container) *[]*gabs.Container {
log.Info("Array Namespace: ", namespace)
log.Info("Container: ", jsonParsed)
log.Debug("Array Namespace: ", namespace)
log.Debug("Container: ", jsonParsed)
value, _ := jsonParsed.Path(namespace).Children()
log.Info("Array value:", value)
log.Debug("Array value:", value)
return &value
}

Expand All @@ -90,17 +90,17 @@ func (s *ServiceDiscovery) GetPortFromObject(host *string, obj *gabs.Container)
}

*host += ":" + portToUse
log.Info("Host: ", *host)
log.Debug("Host: ", *host)
}
}

func (s *ServiceDiscovery) GetNestedObject(item *gabs.Container) string {
log.Info("Parent Data: ", item)
log.Debug("Parent Data: ", item)
parentData := s.decodeToNameSpace(s.parentPath, item)
// Get the data path from the decoded object
subContainer := gabs.Container{}
s.ParseObject(parentData.(string), &subContainer)
log.Info("Parent SubContainer: ", subContainer)
log.Debug("Parent SubContainer: ", subContainer)
// Get the hostname
hostname := s.decodeToNameSpace(s.dataPath, &subContainer).(string)
// Get the port
Expand Down Expand Up @@ -141,7 +141,7 @@ func (s *ServiceDiscovery) GetSubObjectFromList(objList *gabs.Container) *[]stri
if s.endpointReturnsList {
// pre-process the object since we've nested it
thisSet = s.decodeToNameSpaceAsArray(ARRAY_NAME, objList)
log.Info("thisSet: ", thisSet)
log.Debug("thisSet: ", thisSet)
} else {
// It's an object, but the value may be nested
if s.isNested {
Expand All @@ -152,32 +152,32 @@ func (s *ServiceDiscovery) GetSubObjectFromList(objList *gabs.Container) *[]stri
// Now check if this string is a list
nestedString := parentData.(string)
if s.isList(nestedString) {
log.Warning("Yup, it's a list")
log.Debug("Yup, it's a list")
s.ConvertRawListToObj(&nestedString)
s.ParseObject(nestedString, &subContainer)
thisSet = s.decodeToNameSpaceAsArray(ARRAY_NAME, &subContainer)

// Hijack this here because we need to use a non-nested get
for _, item := range *thisSet {
log.Info("Child in list: ", item)
log.Debug("Child in list: ", item)
hostname = s.GetObject(item)
// Add to list
hostList = append(hostList, hostname)
}
return &hostList

} else {
log.Warning("Not a list")
log.Debug("Not a list")
s.ParseObject(parentData.(string), &subContainer)
thisSet = s.decodeToNameSpaceAsArray(s.dataPath, objList)
log.Info("thisSet (object list): ", objList)
log.Debug("thisSet (object list): ", objList)
}
}

}

for _, item := range *thisSet {
log.Info("Child in list: ", item)
log.Debug("Child in list: ", item)
hostname = s.GetHostname(item)
// Add to list
hostList = append(hostList, hostname)
Expand All @@ -199,12 +199,13 @@ func (s *ServiceDiscovery) ConvertRawListToObj(RawData *string) {
}

func (s *ServiceDiscovery) ParseObject(contents string, jsonParsed *gabs.Container) error {
log.Info("Parsing raw data: ", contents)
log.Debug("Parsing raw data: ", contents)
jp, pErr := gabs.ParseJSON([]byte(contents))
log.Error(pErr)
if pErr != nil {
log.Error(pErr)
}
*jsonParsed = *jp
log.Info("Got:", jsonParsed)
log.Error(pErr)
log.Debug("Got:", jsonParsed)
return pErr
}

Expand All @@ -216,13 +217,18 @@ func (s *ServiceDiscovery) ProcessRawData(rawData string) (interface{}, error) {
if s.endpointReturnsList {
// Convert to an object
s.ConvertRawListToObj(&rawData)
s.ParseObject(rawData, &jsonParsed)
log.Info("Parsed object list: ", jsonParsed)
err := s.ParseObject(rawData, &jsonParsed)
if err != nil {
log.Error("Parse object failed: ", err)
return nil, err
}

log.Debug("Parsed object list: ", jsonParsed)
// Treat JSON as a list and then apply the data path
if s.isTargetList {
// Get all values
hostlist = s.GetSubObjectFromList(&jsonParsed)
log.Info("Host list:", hostlist)
log.Debug("Host list:", hostlist)
return hostlist, nil
}

Expand All @@ -241,10 +247,10 @@ func (s *ServiceDiscovery) ProcessRawData(rawData string) (interface{}, error) {
s.ParseObject(rawData, &jsonParsed)
if s.isTargetList {
// It's a list object
log.Warning("It's a target list - getting sub object from list")
log.Warning("Passing in: ", jsonParsed)
log.Debug("It's a target list - getting sub object from list")
log.Debug("Passing in: ", jsonParsed)
hostlist = s.GetSubObjectFromList(&jsonParsed)
log.Warning("Got from object: ", hostlist)
log.Debug("Got from object: ", hostlist)
return hostlist, nil
}

Expand Down
3 changes: 0 additions & 3 deletions tyk_reverse_proxy_clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ package main
import (
"bytes"
"github.com/gorilla/context"
//"github.com/lonelycode/tykcommon"
"errors"
"github.com/lonelycode/gabs"
"github.com/pmylund/go-cache"
"io"
"io/ioutil"
Expand Down

0 comments on commit 235f127

Please sign in to comment.