gorush/notify/feedback.go

54 lines
921 B
Go
Raw Normal View History

package notify
import (
"bytes"
"errors"
"net"
"net/http"
"time"
2021-07-13 08:32:39 +00:00
"github.com/appleboy/gorush/logx"
)
// DispatchFeedback sends a feedback to the configured gateway.
2021-07-13 08:32:39 +00:00
func DispatchFeedback(log logx.LogPushEntry, url string, timeout int64) error {
if url == "" {
return errors.New("url can't be empty")
}
payload, err := json.Marshal(log)
if err != nil {
return err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
2021-01-23 01:39:06 +00:00
transport := &http.Transport{
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
}
2021-01-23 01:39:06 +00:00
client := &http.Client{
Timeout: time.Duration(timeout) * time.Second,
Transport: transport,
}
resp, err := client.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return err
}
return nil
}