35 lines
680 B
Go
35 lines
680 B
Go
package communicate
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"loafle.com/commons/communicate/events"
|
|
"net/http"
|
|
)
|
|
|
|
type communicator struct {
|
|
Queue chan *events.Event
|
|
RootURL string
|
|
}
|
|
|
|
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)
|
|
r, _ := http.Post(m.GetUrl(), "application/json", bytes.NewBuffer(data))
|
|
e.Result <- events.Result{Message: r.Status}
|
|
}(e)
|
|
}
|
|
}()
|
|
}
|