communicate/communicator.go
snoop a4b2278dfd fixed
address
2017-05-29 19:53:29 +09:00

54 lines
1.1 KiB
Go

package communicate
import (
"bytes"
"compress/gzip"
"encoding/json"
"git.loafle.net/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)
}
}()
}