pubsub messagaing working w/ generator

This commit is contained in:
imw
2019-01-22 16:44:45 +01:00
parent fda66aa1af
commit 90de87f243
6 changed files with 54 additions and 26 deletions

View File

@@ -7,9 +7,11 @@ import (
"io"
"github.com/vocdoni/dvote-relay/batch"
"github.com/vocdoni/dvote-relay/types"
"github.com/vocdoni/dvote-relay/data"
)
func parse(rw http.ResponseWriter, request *http.Request) {
decoder := json.NewDecoder(request.Body)
@@ -51,6 +53,39 @@ func parse(rw http.ResponseWriter, request *http.Request) {
io.WriteString(rw, string(j))
}
func Sub(topic string) error {
subscription := data.PsSubscribe(topic)
fmt.Println("Subscribed > " + topic)
for {
msg, err := subscription.Next()
if err != nil {
return err
}
payload := msg.Data()
var e types.Envelope
var b types.Ballot
err = json.Unmarshal(payload, &e)
if err != nil {
return err
}
err = json.Unmarshal(e.Ballot, &b)
if err != nil {
return err
}
err = batch.Add(b)
if err != nil {
return err
}
fmt.Println("Got > " + string(payload))
}
}
func Listen(port string) {
http.HandleFunc("/submit", parse)
//add waitgroup