mirror of
https://github.com/lzh-1625/go_process_manager.git
synced 2025-10-04 23:52:53 +08:00
remove grequests import
This commit is contained in:
@@ -1,30 +1,50 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/lzh-1625/go_process_manager/internal/app/repository"
|
||||
|
||||
"github.com/levigross/grequests"
|
||||
"github.com/lzh-1625/go_process_manager/log"
|
||||
)
|
||||
|
||||
type pushLogic struct{}
|
||||
type pushLogic struct {
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
var PushLogic = new(pushLogic)
|
||||
var PushLogic = &pushLogic{
|
||||
httpClient: &http.Client{
|
||||
Transport: http.DefaultTransport,
|
||||
},
|
||||
}
|
||||
|
||||
func (p *pushLogic) Push(ids []int, placeholders map[string]string) {
|
||||
pl := repository.PushRepository.GetPushConfigByIds(ids)
|
||||
for _, v := range pl {
|
||||
if v.Enable {
|
||||
if v.Method == http.MethodGet {
|
||||
grequests.Get(p.getReplaceMessage(placeholders, v.Url), nil)
|
||||
}
|
||||
var resp *http.Response
|
||||
var reader io.Reader = nil
|
||||
var url string = v.Url
|
||||
if v.Method == http.MethodPost {
|
||||
grequests.Post(v.Url, &grequests.RequestOptions{
|
||||
JSON: p.getReplaceMessage(placeholders, v.Body),
|
||||
})
|
||||
reader = strings.NewReader(p.getReplaceMessage(placeholders, v.Body))
|
||||
}
|
||||
if v.Method == http.MethodGet {
|
||||
url = p.getReplaceMessage(placeholders, url)
|
||||
}
|
||||
req, err := http.NewRequest(v.Method, url, reader)
|
||||
if err != nil {
|
||||
log.Logger.Warnw("推送失败", "err", err, "remark", v.Remark)
|
||||
continue
|
||||
}
|
||||
req.Header.Add("content-type", "application/json")
|
||||
resp, err = p.httpClient.Do(req)
|
||||
if err != nil {
|
||||
log.Logger.Warnw("推送失败", "err", err, "remark", v.Remark)
|
||||
continue
|
||||
}
|
||||
io.Copy(io.Discard, resp.Body)
|
||||
resp.Body.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user