Skip to content

Commit

Permalink
Merge pull request #328 from xzclinux/master
Browse files Browse the repository at this point in the history
Fixed an issue where you need to create a new connection every time y…
  • Loading branch information
feiyu563 committed Jul 13, 2023
2 parents 04bbf5d + 55b25f2 commit 79804fd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
2 changes: 2 additions & 0 deletions conf/app-example.conf
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ to_es_url=http://localhost:9200
# es用户和密码
# to_es_user=username
# to_es_pwd=password
# 长连接最大空闲数
maxIdleConns=100

#---------------------↓webhook-----------------------
#是否开启钉钉告警通道,可同时开始多个通道0为关闭,1为开启
Expand Down
52 changes: 37 additions & 15 deletions controllers/weixin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ type WXMessage struct {
Markdown Mark `json:"markdown"`
}

var client *http.Client

func init() {
var tr *http.Transport
maxIdleConns, _ := beego.AppConfig.Int("MaxIdleConns")
tr = &http.Transport{MaxIdleConns: maxIdleConns}
if proxyUrl := beego.AppConfig.String("proxy"); proxyUrl != "" {
proxy := func(_ *http.Request) (*url.URL, error) {
return url.Parse(proxyUrl)
}
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Proxy: proxy,
}
} else {
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
client = &http.Client{Transport: tr}
}

func PostToWeiXin(text, WXurl, atuserid, logsign string) string {
open := beego.AppConfig.String("open-weixin")
if open != "1" {
Expand All @@ -44,21 +66,21 @@ func PostToWeiXin(text, WXurl, atuserid, logsign string) string {
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(u)
logs.Info(logsign, "[weixin]", b)
var tr *http.Transport
if proxyUrl := beego.AppConfig.String("proxy"); proxyUrl != "" {
proxy := func(_ *http.Request) (*url.URL, error) {
return url.Parse(proxyUrl)
}
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Proxy: proxy,
}
} else {
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
client := &http.Client{Transport: tr}
//var tr *http.Transport
//if proxyUrl := beego.AppConfig.String("proxy"); proxyUrl != "" {
// proxy := func(_ *http.Request) (*url.URL, error) {
// return url.Parse(proxyUrl)
// }
// tr = &http.Transport{
// TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
// Proxy: proxy,
// }
//} else {
// tr = &http.Transport{
// TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
// }
//}
//client := &http.Client{Transport: tr}
res, err := client.Post(WXurl, "application/json", b)
if err != nil {
logs.Error(logsign, "[weixin]", err.Error())
Expand Down

0 comments on commit 79804fd

Please sign in to comment.