mirror of
https://github.com/arnaucube/go-dvote.git
synced 2026-02-28 05:26:46 +01:00
pubsub messagaing working w/ generator
This commit is contained in:
35
net/net.go
35
net/net.go
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user