dao.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go-common/app/job/bbq/video/conf"
  7. notice "go-common/app/service/bbq/notice-service/api/v1"
  8. searchv1 "go-common/app/service/bbq/search/api/grpc/v1"
  9. videov1 "go-common/app/service/bbq/video/api/grpc/v1"
  10. account "go-common/app/service/main/account/api"
  11. "go-common/library/cache/redis"
  12. xsql "go-common/library/database/sql"
  13. "go-common/library/ecode"
  14. "go-common/library/log"
  15. bm "go-common/library/net/http/blademaster"
  16. "go-common/library/net/rpc/warden"
  17. "net/url"
  18. "reflect"
  19. "strconv"
  20. jsoniter "github.com/json-iterator/go"
  21. gomail "gopkg.in/gomail.v2"
  22. )
  23. // Dao dao
  24. type Dao struct {
  25. c *conf.Config
  26. redis *redis.Pool
  27. bfredis *redis.Pool
  28. db *xsql.DB
  29. dbCms *xsql.DB
  30. HTTPClient *bm.Client
  31. SearchClient searchv1.SearchClient
  32. VideoClient videov1.VideoClient
  33. AccountClient account.AccountClient
  34. email *gomail.Dialer
  35. noticeClient notice.NoticeClient
  36. }
  37. // New init mysql db
  38. func New(c *conf.Config) (dao *Dao) {
  39. dao = &Dao{
  40. c: c,
  41. redis: redis.NewPool(c.Redis),
  42. bfredis: redis.NewPool(c.BfRedis),
  43. db: xsql.NewMySQL(c.MySQL),
  44. dbCms: xsql.NewMySQL(c.MySQLCms),
  45. HTTPClient: bm.NewClient(c.BM.Client),
  46. SearchClient: newSearchClient(c.GRPCClient["search"]),
  47. VideoClient: newVideoClient(c.GRPCClient["video"]),
  48. AccountClient: newAccountClient(c.GRPCClient["account"]),
  49. email: gomail.NewDialer(c.Mail.Host, c.Mail.Port, c.Mail.From, c.Mail.Password),
  50. noticeClient: newNoticeClient(c.GRPCClient["notice"]),
  51. }
  52. return
  53. }
  54. // newNoticeClient .
  55. func newNoticeClient(cfg *conf.GRPCConf) notice.NoticeClient {
  56. cc, err := warden.NewClient(cfg.WardenConf).Dial(context.Background(), cfg.Addr)
  57. if err != nil {
  58. panic(err)
  59. }
  60. return notice.NewNoticeClient(cc)
  61. }
  62. //newSearchClient .
  63. func newSearchClient(cfg *conf.GRPCConf) searchv1.SearchClient {
  64. cc, err := warden.NewClient(cfg.WardenConf).Dial(context.Background(), cfg.Addr)
  65. if err != nil {
  66. panic(err)
  67. }
  68. return searchv1.NewSearchClient(cc)
  69. }
  70. //newAccountClient .
  71. func newAccountClient(cfg *conf.GRPCConf) account.AccountClient {
  72. cc, err := warden.NewClient(cfg.WardenConf).Dial(context.Background(), cfg.Addr)
  73. if err != nil {
  74. panic(err)
  75. }
  76. return account.NewAccountClient(cc)
  77. }
  78. //newVideoClient
  79. func newVideoClient(cfg *conf.GRPCConf) videov1.VideoClient {
  80. cc, err := warden.NewClient(cfg.WardenConf).Dial(context.Background(), cfg.Addr)
  81. if err != nil {
  82. panic(err)
  83. }
  84. return videov1.NewVideoClient(cc)
  85. }
  86. // Close close the resource.
  87. func (d *Dao) Close() {
  88. d.redis.Close()
  89. d.db.Close()
  90. }
  91. // Ping dao ping
  92. func (d *Dao) Ping(c context.Context) error {
  93. // TODO: if you need use mc,redis, please add
  94. return d.db.Ping(c)
  95. }
  96. // BeginTran begin mysql transaction
  97. func (d *Dao) BeginTran(c context.Context) (*xsql.Tx, error) {
  98. return d.db.Begin(c)
  99. }
  100. // ReplyHTTPCommon 评论公用请求
  101. func replyHTTPCommon(c context.Context, httpClient *bm.Client, path string, method string, data map[string]interface{}, ip string) (r []byte, err error) {
  102. params := url.Values{}
  103. t := reflect.TypeOf(data).Kind()
  104. if t == reflect.Map {
  105. for k, v := range data {
  106. // params.Set(k, v.(string))
  107. switch reflect.TypeOf(v).Kind() {
  108. case reflect.Int64:
  109. params.Set(k, strconv.FormatInt(v.(int64), 10))
  110. case reflect.Int16:
  111. params.Set(k, strconv.FormatInt(int64(v.(int16)), 10))
  112. case reflect.String:
  113. params.Set(k, v.(string))
  114. case reflect.Int:
  115. params.Set(k, strconv.FormatInt(int64(v.(int)), 10))
  116. }
  117. }
  118. }
  119. log.V(5).Infov(c, log.KV("log", fmt.Sprintf("reply req url(%s)", path+"?"+params.Encode())))
  120. req, err := httpClient.NewRequest(method, path, ip, params)
  121. if err != nil {
  122. log.Errorv(c, log.KV("log", fmt.Sprintf("reply url(%s) error(%v)", path+"?"+params.Encode(), err)))
  123. return
  124. }
  125. var res struct {
  126. Code int `json:"code"`
  127. Msg string `json:"message"`
  128. Data json.RawMessage `json:"data"`
  129. }
  130. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  131. if err = httpClient.Do(c, req, &res); err != nil {
  132. str, _ := json.Marshal(res)
  133. log.Errorv(c, log.KV("log", fmt.Sprintf("reply ret data(%s) err[%v]", str, err)))
  134. return
  135. }
  136. str, _ := json.Marshal(res)
  137. log.V(5).Infov(c, log.KV("log", fmt.Sprintf("reply ret data(%s)", str)))
  138. if res.Code != 0 {
  139. err = ecode.Int(res.Code)
  140. log.Warnv(c, log.KV("log", fmt.Sprintf("reply url(%s) error(%v)", path+"?"+params.Encode(), err)))
  141. }
  142. r = res.Data
  143. return
  144. }