dao.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package monitor
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/md5"
  6. "encoding/json"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "net/http"
  11. "net/url"
  12. "strconv"
  13. "time"
  14. "go-common/app/service/main/up/conf"
  15. "go-common/app/service/main/up/model"
  16. "go-common/library/log"
  17. )
  18. const (
  19. _uri = "/api/v1/message/add"
  20. _method = "POST"
  21. _fileType = "application/json"
  22. )
  23. // Dao is message dao.
  24. type Dao struct {
  25. c *conf.Config
  26. client *http.Client
  27. url string
  28. }
  29. // New new a message dao.
  30. func New(c *conf.Config) (d *Dao) {
  31. d = &Dao{
  32. c: c,
  33. client: &http.Client{
  34. Timeout: time.Duration(time.Second * 1),
  35. },
  36. url: c.Monitor.Host + _uri,
  37. }
  38. return
  39. }
  40. // Send send exception message to owner.
  41. func (d *Dao) Send(c context.Context, username, msg string) (err error) {
  42. params := url.Values{}
  43. now := time.Now().Unix()
  44. params.Set("username", username)
  45. params.Set("content", msg)
  46. params.Set("title", "test")
  47. params.Set("url", "")
  48. params.Set("type", "wechat")
  49. params.Set("token", d.c.Monitor.AppToken)
  50. params.Set("timestamp", strconv.FormatInt(now, 10))
  51. bap := &model.BAP{
  52. UserName: params.Get("username"),
  53. Content: params.Get("content"),
  54. Title: params.Get("title"),
  55. URL: params.Get("url"),
  56. Ty: params.Get("type"),
  57. Token: params.Get("token"),
  58. TimeStamp: now,
  59. Signature: d.getSign(params),
  60. }
  61. jsonStr, err := json.Marshal(bap)
  62. if err != nil {
  63. log.Error("monitor json.Marshal error (%v)", err)
  64. return
  65. }
  66. req, err := http.NewRequest(_method, d.url, bytes.NewBuffer(jsonStr))
  67. if err != nil {
  68. log.Error("monitor http.NewRequest error (%v)", err)
  69. return
  70. }
  71. req.Header.Add("Content-Type", _fileType)
  72. // timeout
  73. ctx, cancel := context.WithTimeout(c, 800*time.Millisecond)
  74. req = req.WithContext(ctx)
  75. defer cancel()
  76. response, err := d.client.Do(req)
  77. if err != nil {
  78. log.Error("monitor d.client.Post error(%v)", err)
  79. return
  80. }
  81. defer response.Body.Close()
  82. if response.StatusCode != http.StatusOK {
  83. log.Error("monitor http.StatusCode nq http.StatusOK (%d) | url(%s)", response.StatusCode, d.url)
  84. return
  85. }
  86. body, err := ioutil.ReadAll(response.Body)
  87. if err != nil {
  88. log.Error("monitor ioutil.ReadAll error(%v)", err)
  89. return
  90. }
  91. var result struct {
  92. Status int `json:"status"`
  93. Msg string `json:"msg"`
  94. }
  95. if err = json.Unmarshal(body, &result); err != nil {
  96. log.Error("monitor json.Unmarshal error(%v)", err)
  97. }
  98. if result.Status != 0 {
  99. log.Error("monitor get status(%d) msg(%s)", result.Status, result.Msg)
  100. }
  101. return
  102. }
  103. func (d *Dao) getSign(params url.Values) (sign string) {
  104. for k, v := range params {
  105. if len(v) == 0 {
  106. params.Del(k)
  107. }
  108. }
  109. h := md5.New()
  110. io.WriteString(h, params.Encode()+d.c.Monitor.AppSecret)
  111. sign = fmt.Sprintf("%x", h.Sum(nil))
  112. return
  113. }