cleaning up

This commit is contained in:
Jan Halfar 2016-03-08 13:27:59 +01:00
parent e0398db7f2
commit f4311a95c3
4 changed files with 60 additions and 43 deletions

View File

@ -1,4 +1,4 @@
language: go language: go
go: go:
- 1.6 - 1.5
- tip - tip

View File

@ -56,7 +56,7 @@ func NewRepo(server string, varDir string) *Repo {
// GetURIs get many uris at once // GetURIs get many uris at once
func (repo *Repo) GetURIs(dimension string, ids []string) map[string]string { func (repo *Repo) GetURIs(dimension string, ids []string) map[string]string {
uris := make(map[string]string) uris := map[string]string{}
for _, id := range ids { for _, id := range ids {
uris[id] = repo.getURI(dimension, id) uris[id] = repo.getURI(dimension, id)
} }
@ -65,7 +65,7 @@ func (repo *Repo) GetURIs(dimension string, ids []string) map[string]string {
// GetNodes get nodes // GetNodes get nodes
func (repo *Repo) GetNodes(r *requests.Nodes) map[string]*content.Node { 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{} path := []*content.Item{}
for nodeName, nodeRequest := range r.Nodes { for nodeName, nodeRequest := range r.Nodes {
log.Debug(" adding node " + nodeName + " " + nodeRequest.ID) 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) log.Warning("could not get dimension root node for nodeRequest.Dimension: " + nodeRequest.Dimension)
continue 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) nodes[nodeName] = repo.getNode(treeNode, nodeRequest.Expand, nodeRequest.MimeTypes, path, 0, r.Env.Groups, nodeRequest.DataFields)
} else { } else {
log.Warning("you are requesting an invalid tree node for " + nodeName + " : " + nodeRequest.ID) 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 { if len(req.URI) == 0 {
return errors.New("request URI must not be empty") return errors.New("request URI must not be empty")
} }
if req.Env == nil { if req.Env == nil {
return errors.New("request.Env must not be nil") return errors.New("request.Env must not be nil")
} }
if len(req.Env.Dimensions) == 0 { if len(req.Env.Dimensions) == 0 {
return errors.New("request.Env.Dimensions must not be empty") return errors.New("request.Env.Dimensions must not be empty")
} }
for _, envDimension := range req.Env.Dimensions { for _, envDimension := range req.Env.Dimensions {
if !repo.hasDimension(envDimension) { if !repo.hasDimension(envDimension) {
availableDimensions := []string{} availableDimensions := []string{}

View File

@ -61,60 +61,72 @@ func (s *socketServer) handleSocketRequest(handler string, jsonBuffer []byte) (r
var apiErr error var apiErr error
var jsonErr error var jsonErr error
log.Record(fmt.Sprintf("socket.handleSocketRequest(%d): %s %s", s.stats.requests, handler, string(jsonBuffer))) 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 { switch handler {
case "getURIs": case "getURIs":
getURIRequest := &requests.URIs{} getURIRequest := &requests.URIs{}
jsonErr = json.Unmarshal(jsonBuffer, &getURIRequest) ifJSONIsFine(json.Unmarshal(jsonBuffer, &getURIRequest), func() {
log.Debug(" getURIRequest: " + fmt.Sprint(getURIRequest)) log.Debug(" getURIRequest: " + fmt.Sprint(getURIRequest))
uris := s.repo.GetURIs(getURIRequest.Dimension, getURIRequest.Ids) uris := s.repo.GetURIs(getURIRequest.Dimension, getURIRequest.Ids)
log.Debug(" resolved: " + fmt.Sprint(uris)) log.Debug(" resolved: " + fmt.Sprint(uris))
reply = uris reply = uris
break })
case "content": case "content":
contentRequest := &requests.Content{} contentRequest := &requests.Content{}
jsonErr = json.Unmarshal(jsonBuffer, &contentRequest) ifJSONIsFine(json.Unmarshal(jsonBuffer, &contentRequest), func() {
log.Debug("contentRequest:", contentRequest) log.Debug("contentRequest:", contentRequest)
content, apiErr := s.repo.GetContent(contentRequest) content, contentAPIErr := s.repo.GetContent(contentRequest)
log.Debug(apiErr) apiErr = contentAPIErr
reply = content reply = content
break })
case "getNodes": case "getNodes":
nodesRequest := &requests.Nodes{} nodesRequest := &requests.Nodes{}
jsonErr = json.Unmarshal(jsonBuffer, &nodesRequest) ifJSONIsFine(json.Unmarshal(jsonBuffer, &nodesRequest), func() {
log.Debug(" nodesRequest: " + fmt.Sprint(nodesRequest)) log.Debug(" nodesRequest: " + fmt.Sprint(nodesRequest))
nodesMap := s.repo.GetNodes(nodesRequest) nodesMap := s.repo.GetNodes(nodesRequest)
reply = nodesMap reply = nodesMap
break })
case "update": case "update":
updateRequest := &requests.Update{} updateRequest := &requests.Update{}
jsonErr = json.Unmarshal(jsonBuffer, &updateRequest) ifJSONIsFine(json.Unmarshal(jsonBuffer, &updateRequest), func() {
log.Debug(" updateRequest: " + fmt.Sprint(updateRequest)) log.Debug(" updateRequest: " + fmt.Sprint(updateRequest))
updateResponse := s.repo.Update() updateResponse := s.repo.Update()
reply = updateResponse reply = updateResponse
break })
case "getRepo": case "getRepo":
repoRequest := &requests.Repo{} repoRequest := &requests.Repo{}
jsonErr = json.Unmarshal(jsonBuffer, &repoRequest) ifJSONIsFine(json.Unmarshal(jsonBuffer, &repoRequest), func() {
log.Debug(" getRepoRequest: " + fmt.Sprint(repoRequest)) log.Debug(" getRepoRequest: " + fmt.Sprint(repoRequest))
repoResponse := s.repo.GetRepo() repoResponse := s.repo.GetRepo()
reply = repoResponse reply = repoResponse
break })
default: default:
err = errors.New(log.Error(" can not handle this one " + handler)) err = errors.New(log.Error(" can not handle this one " + handler))
errorResponse := responses.NewError(1, "unknown handler") errorResponse := responses.NewError(1, "unknown handler")
reply = errorResponse reply = errorResponse
} }
if jsonErr != nil { 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()) errorResponse := responses.NewError(2, "could not read incoming json "+jsonErr.Error())
reply = errorResponse reply = errorResponse
} else if apiErr != nil { } else if apiErr != nil {
log.Error(" an API error occured:", apiErr)
err = apiErr
reply = responses.NewError(3, "internal error "+apiErr.Error()) reply = responses.NewError(3, "internal error "+apiErr.Error())
} }
encodedBytes, jsonErr := json.MarshalIndent(map[string]interface{}{"reply": reply}, "", " ") encodedBytes, jsonReplyErr := json.MarshalIndent(map[string]interface{}{"reply": reply}, "", " ")
if jsonErr != nil { if jsonReplyErr != nil {
err = jsonErr err = jsonReplyErr
log.Error(" could not encode reply " + fmt.Sprint(jsonErr)) log.Error(" could not encode reply " + fmt.Sprint(jsonReplyErr))
} else { } else {
replyBytes = encodedBytes replyBytes = encodedBytes
} }
@ -126,14 +138,15 @@ func (s *socketServer) handleConnection(conn net.Conn) {
var headerBuffer [1]byte var headerBuffer [1]byte
header := "" header := ""
for { for {
// let us read with 1 byte steps on conn until we find "{"
_, readErr := conn.Read(headerBuffer[0:]) _, readErr := conn.Read(headerBuffer[0:])
if readErr != nil { if readErr != nil {
log.Debug(" looks like the client closed the connection - this is my readError: " + fmt.Sprint(readErr)) log.Debug(" looks like the client closed the connection - this is my readError: " + fmt.Sprint(readErr))
return return
} }
// read next byte // read next byte
current := string(headerBuffer[0:]) current := headerBuffer[0:]
if current == "{" { if string(current) == "{" {
// json has started // json has started
headerParts := strings.Split(header, ":") headerParts := strings.Split(header, ":")
header = "" header = ""
@ -141,17 +154,23 @@ func (s *socketServer) handleConnection(conn net.Conn) {
jsonLength, _ := strconv.Atoi(headerParts[1]) jsonLength, _ := strconv.Atoi(headerParts[1])
log.Debug(fmt.Sprintf(" found json with %d bytes", jsonLength)) log.Debug(fmt.Sprintf(" found json with %d bytes", jsonLength))
if jsonLength > 0 { if jsonLength > 0 {
// let us try to read some json
jsonBuffer := make([]byte, jsonLength) jsonBuffer := make([]byte, jsonLength)
// that is "{"
jsonBuffer[0] = 123 jsonBuffer[0] = 123
_, jsonReadErr := conn.Read(jsonBuffer[1:]) _, jsonReadErr := conn.Read(jsonBuffer[1:])
if jsonReadErr != nil { if jsonReadErr != nil {
log.Error(" could not read json - giving up with this client connection" + fmt.Sprint(jsonReadErr)) log.Error(" could not read json - giving up with this client connection" + fmt.Sprint(jsonReadErr))
return 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) reply, handlingError := s.handleSocketRequest(requestHandler, jsonBuffer)
if handlingError != nil { if handlingError != nil {
log.Error("socket.handleConnection: handlingError " + fmt.Sprint(handlingError)) log.Error("socket.handleConnection handlingError :", handlingError)
if reply == nil { if reply == nil {
log.Error("giving up with nil reply") log.Error("giving up with nil reply")
conn.Close() conn.Close()

View File

@ -1 +0,0 @@
package server