55 lines
1.0 KiB
Go
55 lines
1.0 KiB
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 CompressDataGzip(data []byte) []byte {
|
|
// var b bytes.Buffer
|
|
// w := gzip.NewWriter(&b)
|
|
// w.Write(data)
|
|
// w.Close()
|
|
// return b.Bytes()
|
|
//}
|
|
|
|
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)
|
|
|
|
// compress , accept-encoding : gzip
|
|
|
|
//res, err := http.Post(GetRootURL()+m.GetUrl(), "application/json", bytes.NewBuffer(CompressDataGzip(data)))
|
|
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)
|
|
}
|
|
}()
|
|
}
|