communicate/communicator.go

42 lines
765 B
Go
Raw Normal View History

2016-11-28 14:13:15 +09:00
package communicate
import (
"bytes"
"encoding/json"
"loafle.com/commons/communicate/events"
"net/http"
)
type communicator struct {
2016-11-28 14:43:22 +09:00
Queue chan *events.Event
RootURL string
2016-11-28 14:13:15 +09: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-30 14:44:35 +09:00
res, err := http.Post(GetRootURL()+m.GetUrl(), "application/json", bytes.NewBuffer(data))
// todo timeout,error
if err != nil {
}
if res.StatusCode != 200 && res.StatusCode != 201 {
}
2016-11-28 14:13:15 +09:00
}(e)
}
}()
}