hook_url.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "reflect"
  9. "strings"
  10. "sync"
  11. "go-common/app/admin/ep/tapd/model"
  12. "go-common/library/cache/memcache"
  13. "go-common/library/log"
  14. )
  15. var rwmLock = new(sync.RWMutex)
  16. // CallHookUrl Call Hook Url.
  17. func (d *Dao) CallHookUrl(c context.Context, URL string, body interface{}) (err error) {
  18. var (
  19. req *http.Request
  20. res = make(map[string]interface{})
  21. )
  22. if req, err = d.newRequest(http.MethodPost, URL, body); err != nil {
  23. log.Error("d.CallHookUrl url(%s) err(%v)", URL, err)
  24. return
  25. }
  26. if err = d.httpClient.Do(c, req, &res); err != nil {
  27. log.Error("d.CallHookUrl url(%s) err(%v)", URL, err)
  28. }
  29. return
  30. }
  31. // CallHookUrlAsForm Call Hook Url As Form.
  32. func (d *Dao) CallHookUrlAsForm(c context.Context, URL string, body map[string]interface{}) (err error) {
  33. var (
  34. res = make(map[string]interface{})
  35. req *http.Request
  36. )
  37. data := make(url.Values)
  38. for mapKey := range body {
  39. typeKind := reflect.TypeOf(body[mapKey]).Kind()
  40. switch typeKind {
  41. case reflect.Int:
  42. data[mapKey] = []string{fmt.Sprintf("%d", body[mapKey].(int))}
  43. case reflect.Int64:
  44. data[mapKey] = []string{fmt.Sprintf("%d", body[mapKey].(int64))}
  45. case reflect.Float64:
  46. data[mapKey] = []string{fmt.Sprintf("%.0f", body[mapKey].(float64))}
  47. case reflect.String:
  48. data[mapKey] = []string{body[mapKey].(string)}
  49. default:
  50. data[mapKey] = []string{fmt.Sprint(body[mapKey])}
  51. }
  52. }
  53. if req, err = http.NewRequest(http.MethodPost, URL, strings.NewReader(data.Encode())); err != nil {
  54. return
  55. }
  56. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  57. err = d.httpClient.Do(c, req, &res)
  58. return
  59. }
  60. // SaveEnableHookURLToCache Save Enable Hook URL To Cache.
  61. func (d *Dao) SaveEnableHookURLToCache() (eventMap map[string][]*model.HookUrl, err error) {
  62. rwmLock.Lock()
  63. defer rwmLock.Unlock()
  64. var urlEvents []*model.UrlEvent
  65. eventMap = make(map[string][]*model.HookUrl)
  66. // 获取有效event
  67. if urlEvents, err = d.QueryURLEventByStatus(model.HookEventStatusEnable); err != nil {
  68. return
  69. }
  70. //倒查url 过滤不存在或无效状态
  71. for _, urlEvent := range urlEvents {
  72. var hookURL *model.HookUrl
  73. if hookURL, err = d.QueryHookURLByID(urlEvent.UrlID); err != nil || hookURL.ID == 0 || hookURL.Status != model.HookURLStatusEnable {
  74. continue
  75. }
  76. eventMap[urlEvent.Event] = append(eventMap[urlEvent.Event], hookURL)
  77. }
  78. for eventMapKey := range eventMap {
  79. item := &memcache.Item{Key: eventMapKey, Object: eventMap[eventMapKey], Flags: memcache.FlagJSON, Expiration: d.expire}
  80. d.tokenCacheSave(context.Background(), item)
  81. }
  82. return
  83. }
  84. // GetEnableHookURLFromCacheAndSaveIfNot Get Enable Hook URL From Cache and save if not.
  85. func (d *Dao) GetEnableHookURLFromCacheAndSaveIfNot(event model.Event) (hookURLs []*model.HookUrl, err error) {
  86. var (
  87. conn = d.mc.Get(context.Background())
  88. item *memcache.Item
  89. )
  90. rwmLock.RLock()
  91. defer rwmLock.RUnlock()
  92. defer conn.Close()
  93. if item, err = conn.Get(string(event)); err == nil {
  94. if err = json.Unmarshal(item.Value, &hookURLs); err != nil {
  95. log.Error("GetEnableHookURLFromCache json parse error(%v)", err)
  96. }
  97. return
  98. }
  99. var eventMap map[string][]*model.HookUrl
  100. if eventMap, err = d.SaveEnableHookURLToCache(); err != nil {
  101. return
  102. }
  103. hookURLs = eventMap[string(event)]
  104. return
  105. }
  106. // GetEnableHookURLFromCache Get Enable Hook URL From Cache.
  107. func (d *Dao) GetEnableHookURLFromCache(event model.Event) (hookURLs []*model.HookUrl, err error) {
  108. var (
  109. conn = d.mc.Get(context.Background())
  110. item *memcache.Item
  111. )
  112. rwmLock.RLock()
  113. defer rwmLock.RUnlock()
  114. defer conn.Close()
  115. if item, err = conn.Get(string(event)); err == nil {
  116. if err = json.Unmarshal(item.Value, &hookURLs); err != nil {
  117. log.Error("GetEnableHookURLFromCache json parse error(%v)", err)
  118. }
  119. return
  120. }
  121. return
  122. }
  123. // GetEnableHookURLFromDB Get Enable Hook URL From DB.
  124. func (d *Dao) GetEnableHookURLFromDB(event model.Event) (hookURLs []*model.HookUrl, err error) {
  125. var urlEvents []*model.UrlEvent
  126. if urlEvents, err = d.QueryURLEventByEventAndStatus(string(event), model.HookEventStatusEnable); err != nil {
  127. return
  128. }
  129. for _, urlEvent := range urlEvents {
  130. var hookURL *model.HookUrl
  131. if hookURL, err = d.QueryHookURLByID(urlEvent.UrlID); err != nil {
  132. return
  133. }
  134. hookURLs = append(hookURLs, hookURL)
  135. }
  136. return
  137. }