From f4311a95c338c6aad6d05ded9cfd9c02ac0a9df4 Mon Sep 17 00:00:00 2001 From: Jan Halfar Date: Tue, 8 Mar 2016 13:27:59 +0100 Subject: [PATCH] cleaning up --- .travis.yml | 2 +- repo/repo.go | 9 +++-- server/server.go | 91 +++++++++++++++++++++++++++++------------------- server/socket.go | 1 - 4 files changed, 60 insertions(+), 43 deletions(-) delete mode 100644 server/socket.go diff --git a/.travis.yml b/.travis.yml index be627f3..9244d77 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,4 @@ language: go go: - - 1.6 + - 1.5 - tip diff --git a/repo/repo.go b/repo/repo.go index dc50bf7..953a904 100644 --- a/repo/repo.go +++ b/repo/repo.go @@ -56,7 +56,7 @@ func NewRepo(server string, varDir string) *Repo { // GetURIs get many uris at once func (repo *Repo) GetURIs(dimension string, ids []string) map[string]string { - uris := make(map[string]string) + uris := map[string]string{} for _, id := range ids { uris[id] = repo.getURI(dimension, id) } @@ -65,7 +65,7 @@ func (repo *Repo) GetURIs(dimension string, ids []string) map[string]string { // GetNodes get nodes func (repo *Repo) GetNodes(r *requests.Nodes) map[string]*content.Node { - nodes := make(map[string]*content.Node) + nodes := map[string]*content.Node{} path := []*content.Item{} for nodeName, nodeRequest := range r.Nodes { log.Debug(" adding node " + nodeName + " " + nodeRequest.ID) @@ -75,7 +75,8 @@ func (repo *Repo) GetNodes(r *requests.Nodes) map[string]*content.Node { log.Warning("could not get dimension root node for nodeRequest.Dimension: " + nodeRequest.Dimension) continue } - if treeNode, ok := dimensionNode.Directory[nodeRequest.ID]; ok { + treeNode, ok := dimensionNode.Directory[nodeRequest.ID] + if ok { nodes[nodeName] = repo.getNode(treeNode, nodeRequest.Expand, nodeRequest.MimeTypes, path, 0, r.Env.Groups, nodeRequest.DataFields) } else { log.Warning("you are requesting an invalid tree node for " + nodeName + " : " + nodeRequest.ID) @@ -271,14 +272,12 @@ func (repo *Repo) validateContentRequest(req *requests.Content) (err error) { if len(req.URI) == 0 { return errors.New("request URI must not be empty") } - if req.Env == nil { return errors.New("request.Env must not be nil") } if len(req.Env.Dimensions) == 0 { return errors.New("request.Env.Dimensions must not be empty") } - for _, envDimension := range req.Env.Dimensions { if !repo.hasDimension(envDimension) { availableDimensions := []string{} diff --git a/server/server.go b/server/server.go index 4eadb3e..1589718 100644 --- a/server/server.go +++ b/server/server.go @@ -61,60 +61,72 @@ func (s *socketServer) handleSocketRequest(handler string, jsonBuffer []byte) (r var apiErr error var jsonErr error log.Record(fmt.Sprintf("socket.handleSocketRequest(%d): %s %s", s.stats.requests, handler, string(jsonBuffer))) + + ifJSONIsFine := func(err error, processingFunc func()) { + if err != nil { + jsonErr = err + return + } + processingFunc() + } + switch handler { case "getURIs": getURIRequest := &requests.URIs{} - jsonErr = json.Unmarshal(jsonBuffer, &getURIRequest) - log.Debug(" getURIRequest: " + fmt.Sprint(getURIRequest)) - uris := s.repo.GetURIs(getURIRequest.Dimension, getURIRequest.Ids) - log.Debug(" resolved: " + fmt.Sprint(uris)) - reply = uris - break + ifJSONIsFine(json.Unmarshal(jsonBuffer, &getURIRequest), func() { + log.Debug(" getURIRequest: " + fmt.Sprint(getURIRequest)) + uris := s.repo.GetURIs(getURIRequest.Dimension, getURIRequest.Ids) + log.Debug(" resolved: " + fmt.Sprint(uris)) + reply = uris + }) case "content": contentRequest := &requests.Content{} - jsonErr = json.Unmarshal(jsonBuffer, &contentRequest) - log.Debug("contentRequest:", contentRequest) - content, apiErr := s.repo.GetContent(contentRequest) - log.Debug(apiErr) - reply = content - break + ifJSONIsFine(json.Unmarshal(jsonBuffer, &contentRequest), func() { + log.Debug("contentRequest:", contentRequest) + content, contentAPIErr := s.repo.GetContent(contentRequest) + apiErr = contentAPIErr + reply = content + }) case "getNodes": nodesRequest := &requests.Nodes{} - jsonErr = json.Unmarshal(jsonBuffer, &nodesRequest) - log.Debug(" nodesRequest: " + fmt.Sprint(nodesRequest)) - nodesMap := s.repo.GetNodes(nodesRequest) - reply = nodesMap - break + ifJSONIsFine(json.Unmarshal(jsonBuffer, &nodesRequest), func() { + log.Debug(" nodesRequest: " + fmt.Sprint(nodesRequest)) + nodesMap := s.repo.GetNodes(nodesRequest) + reply = nodesMap + }) case "update": updateRequest := &requests.Update{} - jsonErr = json.Unmarshal(jsonBuffer, &updateRequest) - log.Debug(" updateRequest: " + fmt.Sprint(updateRequest)) - updateResponse := s.repo.Update() - reply = updateResponse - break + ifJSONIsFine(json.Unmarshal(jsonBuffer, &updateRequest), func() { + log.Debug(" updateRequest: " + fmt.Sprint(updateRequest)) + updateResponse := s.repo.Update() + reply = updateResponse + }) case "getRepo": repoRequest := &requests.Repo{} - jsonErr = json.Unmarshal(jsonBuffer, &repoRequest) - log.Debug(" getRepoRequest: " + fmt.Sprint(repoRequest)) - repoResponse := s.repo.GetRepo() - reply = repoResponse - break + ifJSONIsFine(json.Unmarshal(jsonBuffer, &repoRequest), func() { + log.Debug(" getRepoRequest: " + fmt.Sprint(repoRequest)) + repoResponse := s.repo.GetRepo() + reply = repoResponse + }) default: err = errors.New(log.Error(" can not handle this one " + handler)) errorResponse := responses.NewError(1, "unknown handler") reply = errorResponse } if jsonErr != nil { - log.Error(" could not read incoming json: " + fmt.Sprint(jsonErr)) + err = jsonErr + log.Error(" could not read incoming json:", jsonErr) errorResponse := responses.NewError(2, "could not read incoming json "+jsonErr.Error()) reply = errorResponse } else if apiErr != nil { + log.Error(" an API error occured:", apiErr) + err = apiErr reply = responses.NewError(3, "internal error "+apiErr.Error()) } - encodedBytes, jsonErr := json.MarshalIndent(map[string]interface{}{"reply": reply}, "", " ") - if jsonErr != nil { - err = jsonErr - log.Error(" could not encode reply " + fmt.Sprint(jsonErr)) + encodedBytes, jsonReplyErr := json.MarshalIndent(map[string]interface{}{"reply": reply}, "", " ") + if jsonReplyErr != nil { + err = jsonReplyErr + log.Error(" could not encode reply " + fmt.Sprint(jsonReplyErr)) } else { replyBytes = encodedBytes } @@ -126,14 +138,15 @@ func (s *socketServer) handleConnection(conn net.Conn) { var headerBuffer [1]byte header := "" for { + // let us read with 1 byte steps on conn until we find "{" _, readErr := conn.Read(headerBuffer[0:]) if readErr != nil { log.Debug(" looks like the client closed the connection - this is my readError: " + fmt.Sprint(readErr)) return } // read next byte - current := string(headerBuffer[0:]) - if current == "{" { + current := headerBuffer[0:] + if string(current) == "{" { // json has started headerParts := strings.Split(header, ":") header = "" @@ -141,17 +154,23 @@ func (s *socketServer) handleConnection(conn net.Conn) { jsonLength, _ := strconv.Atoi(headerParts[1]) log.Debug(fmt.Sprintf(" found json with %d bytes", jsonLength)) if jsonLength > 0 { + // let us try to read some json jsonBuffer := make([]byte, jsonLength) + // that is "{" jsonBuffer[0] = 123 _, jsonReadErr := conn.Read(jsonBuffer[1:]) if jsonReadErr != nil { log.Error(" could not read json - giving up with this client connection" + fmt.Sprint(jsonReadErr)) return } - log.Debug(" read json: " + string(jsonBuffer)) + if log.SelectedLevel == log.LevelDebug { + log.Debug(" read json: " + string(jsonBuffer)) + } + + // execution time reply, handlingError := s.handleSocketRequest(requestHandler, jsonBuffer) if handlingError != nil { - log.Error("socket.handleConnection: handlingError " + fmt.Sprint(handlingError)) + log.Error("socket.handleConnection handlingError :", handlingError) if reply == nil { log.Error("giving up with nil reply") conn.Close() diff --git a/server/socket.go b/server/socket.go deleted file mode 100644 index abb4e43..0000000 --- a/server/socket.go +++ /dev/null @@ -1 +0,0 @@ -package server