1
0

NotifyCenter.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package notify_center
  2. import (
  3. "github.com/go-resty/resty/v2"
  4. "github.com/sirupsen/logrus"
  5. "net/http"
  6. "net/url"
  7. "sync"
  8. )
  9. type NotifyCenter struct {
  10. log *logrus.Logger
  11. webhookUrl string
  12. infos map[string]string
  13. mu sync.Mutex
  14. }
  15. func NewNotifyCenter(log *logrus.Logger, webhookUrl string) *NotifyCenter {
  16. n := NotifyCenter{log: log, webhookUrl: webhookUrl}
  17. n.infos = make(map[string]string)
  18. return &n
  19. }
  20. func (n *NotifyCenter) Add(groupName, infoContent string) {
  21. if n == nil {
  22. return
  23. }
  24. n.mu.Lock()
  25. defer n.mu.Unlock()
  26. n.infos[groupName] = infoContent
  27. }
  28. func (n *NotifyCenter) Send() {
  29. if n == nil || n.webhookUrl == "" {
  30. return
  31. }
  32. client := resty.New().SetTransport(&http.Transport{
  33. DisableKeepAlives: true,
  34. MaxIdleConns: 100,
  35. MaxIdleConnsPerHost: 100,
  36. })
  37. for s, s2 := range n.infos {
  38. _, err := client.R().Get(n.webhookUrl + s + "/" + url.QueryEscape(s2))
  39. if err != nil {
  40. n.log.Errorln("NewNotifyCenter.Send", err)
  41. return
  42. }
  43. }
  44. }
  45. func (n *NotifyCenter) Clear() {
  46. if n == nil {
  47. return
  48. }
  49. n.infos = make(map[string]string)
  50. }
  51. var Notify *NotifyCenter