dao.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package alarm
  2. import (
  3. "context"
  4. "net/http"
  5. "strings"
  6. "time"
  7. "go-common/app/service/main/resource/conf"
  8. "go-common/app/service/main/resource/model"
  9. "go-common/library/log"
  10. httpx "go-common/library/net/http/blademaster"
  11. )
  12. // Dao is redis dao.
  13. type Dao struct {
  14. c *conf.Config
  15. netClient *http.Client
  16. httpClient *httpx.Client
  17. }
  18. // New is new redis dao.
  19. func New(c *conf.Config) (d *Dao) {
  20. d = &Dao{
  21. c: c,
  22. httpClient: httpx.NewClient(c.HTTPClient),
  23. netClient: &http.Client{
  24. Timeout: 3 * time.Second,
  25. },
  26. }
  27. return d
  28. }
  29. func (d *Dao) CheckURL(originURL string, wis []*model.ResWarnInfo) {
  30. var (
  31. url string
  32. req *http.Request
  33. resp *http.Response
  34. err error
  35. )
  36. if strings.HasPrefix(originURL, "https://") {
  37. log.Info("CheckURL url(%s) is https ,replace to http", originURL)
  38. url = strings.Replace(originURL, "https://", "http://", -1)
  39. } else if !strings.HasPrefix(originURL, "http://") {
  40. log.Info("CheckURL url(%s) don't have https and http", originURL)
  41. url = "http://" + originURL
  42. } else {
  43. url = originURL
  44. }
  45. if req, err = http.NewRequest("GET", url, nil); err != nil {
  46. log.Error("CheckURL NewRequest(%v) error(%v)", url, err)
  47. return
  48. }
  49. req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
  50. req.Header.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36")
  51. resp, err = d.netClient.Do(req)
  52. if err != nil {
  53. log.Error("CheckURL url(%s) originURL(%s) error(%v)", url, originURL, err)
  54. } else if resp.StatusCode != http.StatusOK {
  55. log.Error("CheckURL url(%s) originURL(%s) code(%v) not OK ", url, originURL, resp.StatusCode)
  56. var sends = make(map[string][]*model.ResWarnInfo)
  57. for _, wi := range wis {
  58. sends[wi.UserName] = append(sends[wi.UserName], wi)
  59. }
  60. for userName, send := range sends {
  61. d.sendWeChartURL(context.TODO(), resp.StatusCode, userName, send)
  62. }
  63. }
  64. }