12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package alarm_msg
- import (
- "crypto/tls"
- "encoding/json"
- "eta/eta_forum_hub/utils"
- "fmt"
- "io/ioutil"
- "net/http"
- "strings"
- "time"
- )
- // SendAlarmMsg
- // projectName-项目名称
- // runMode-运行模式
- // msgBody-消息内容
- // level:消息基本,1:提示消息,2:警告消息,3:严重错误信息,默认为1 提示消息
- func SendAlarmMsg(msgBody string, level int) {
- if utils.AlarmMsgUrl == `` {
- return
- }
- params := make(map[string]interface{})
- params["ProjectName"] = utils.APP_NAME_CN
- params["RunMode"] = utils.RunMode
- params["MsgBody"] = msgBody
- params["Level"] = level
- param, err := json.Marshal(params)
- if err != nil {
- utils.FileLog.Info("SendAlarmMsg json.Marshal Err:" + err.Error())
- return
- }
- _, _ = Post(utils.AlarmMsgUrl, string(param), "application/json")
- }
- func HttpPost(url, postData string, params ...string) ([]byte, error) {
- contentType := "application/x-www-form-urlencoded;charset=utf-8"
- if len(params) > 0 && params[0] != "" {
- contentType = params[0]
- }
- sTime := time.Now()
- resp, err := http.Post(url,
- contentType,
- strings.NewReader(postData))
- eTime := time.Now()
- t := eTime.Sub(sTime).Nanoseconds() / 1000000
- if err != nil {
- return nil, err
- }
- defer resp.Body.Close()
- b, err := ioutil.ReadAll(resp.Body)
- fmt.Println(t, "ms", "POST", "URL", url, "DATA", postData, "RESPONSE", string(b))
- return b, err
- }
- func Post(url, postData string, params ...string) ([]byte, error) {
- if strings.HasPrefix(url, "https://") {
- return HttpsPost(url, postData, params...)
- } else {
- return HttpPost(url, postData, params...)
- }
- }
- func HttpsPost(url, postData string, params ...string) ([]byte, error) {
- body := ioutil.NopCloser(strings.NewReader(postData))
- tr := &http.Transport{
- TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
- DisableCompression: true,
- }
- client := &http.Client{Transport: tr}
- req, err := http.NewRequest("POST", url, body)
- if err != nil {
- return nil, err
- }
- contentType := "application/x-www-form-urlencoded;charset=utf-8"
- if len(params) > 0 && params[0] != "" {
- contentType = params[0]
- }
- req.Header.Set("Content-Type", contentType)
- sTime := time.Now()
- resp, err := client.Do(req)
- eTime := time.Now()
- t := eTime.Sub(sTime).Nanoseconds() / 1000000
- if err != nil {
- return nil, err
- }
- defer resp.Body.Close()
- b, err := ioutil.ReadAll(resp.Body)
- fmt.Println(t, "ms", "POST", "URL", url, "DATA", postData, "RESPONSE", string(b))
- return b, err
- }
|