NotifyCenter.go 892 B

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