NotifyCenter.go 977 B

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