communicate/communicator.go
2016-11-30 14:44:35 +09:00

42 lines
765 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)
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 {
}
}(e)
}
}()
}