communicate/communicator.go

36 lines
722 B
Go
Raw Normal View History

2016-11-28 05:13:15 +00:00
package communicate
import (
"bytes"
"encoding/json"
"loafle.com/commons/communicate/events"
"net/http"
2016-11-28 06:08:17 +00:00
"strconv"
2016-11-28 05:13:15 +00:00
)
type communicator struct {
2016-11-28 05:43:22 +00:00
Queue chan *events.Event
RootURL string
2016-11-28 05:13:15 +00:00
}
func NewCommunicator() *communicator {
return &communicator{Queue: make(chan *events.Event, 10)}
}
func (c *communicator) addEvent(e *events.Event) {
c.Queue <- e
}
func (c *communicator) start() {
go func() {
for e := range c.Queue {
go func(event *events.Event) {
m := event.Data.(events.URLMaker)
data, _ := json.Marshal(event)
2016-11-28 06:00:12 +00:00
r, _ := http.Post(GetRootURL()+m.GetUrl(), "application/json", bytes.NewBuffer(data))
2016-11-28 06:08:17 +00:00
e.Result <- events.Result{Message: strconv.Itoa(r.StatusCode)}
2016-11-28 05:13:15 +00:00
}(e)
}
}()
}