1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2024-12-14 12:37:31 +00:00

added reading logic to httpListener

This commit is contained in:
postmannen 2021-09-10 06:06:43 +02:00
parent 08dfcfb3c3
commit 7576619172

View file

@ -139,14 +139,44 @@ func (s *server) writeStewSocket(toStewSocketCh []byte) {
}
func (s *server) readHTTPlistenerHandler(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
if err != nil {
er := fmt.Errorf("error: readHTTPListenerHandler: %v", err)
sendErrorLogMessage(s.configuration, s.metrics, s.newMessagesCh, Node(s.nodeName), er)
}
r.Body.Close()
log.Printf("got: %v\n", string(b))
var readBytes []byte
for {
b := make([]byte, 1500)
_, err := r.Body.Read(b)
if err != nil && err != io.EOF {
er := fmt.Errorf("error: failed to read data from tcp listener: %v", err)
sendErrorLogMessage(s.configuration, s.metrics, s.newMessagesCh, Node(s.nodeName), er)
return
}
readBytes = append(readBytes, b...)
if err == io.EOF {
break
}
}
readBytes = bytes.Trim(readBytes, "\x00")
// unmarshal the JSON into a struct
sam, err := s.convertBytesToSAMs(readBytes)
if err != nil {
er := fmt.Errorf("error: malformed json: %v", err)
sendErrorLogMessage(s.configuration, s.metrics, s.newMessagesCh, Node(s.nodeName), er)
return
}
for i := range sam {
// Fill in the value for the FromNode field, so the receiver
// can check this field to know where it came from.
sam[i].Message.FromNode = Node(s.nodeName)
}
// Send the SAM struct to be picked up by the ring buffer.
s.newMessagesCh <- sam
}