dao.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package dao
  2. import (
  3. "context"
  4. xhttp "net/http"
  5. "time"
  6. "go-common/app/admin/main/member/conf"
  7. "go-common/app/admin/main/member/dao/block"
  8. "go-common/library/cache/memcache"
  9. "go-common/library/cache/redis"
  10. "go-common/library/database/elastic"
  11. "go-common/library/database/orm"
  12. "go-common/library/database/sql"
  13. bm "go-common/library/net/http/blademaster"
  14. "go-common/library/queue/databus"
  15. "go-common/library/database/hbase.v2"
  16. "github.com/jinzhu/gorm"
  17. )
  18. // Dao dao
  19. type Dao struct {
  20. c *conf.Config
  21. // db
  22. member *gorm.DB
  23. memberRead *gorm.DB
  24. account *gorm.DB
  25. block *block.Dao
  26. fhbyophbase *hbase.Client
  27. fhbymidhbase *hbase.Client
  28. httpClient *bm.Client
  29. passportClient *bm.Client
  30. bfsclient *xhttp.Client
  31. upUnameURL string
  32. queryByMidsURL string
  33. msgURL string
  34. expMsgDatabus *databus.Databus
  35. es *elastic.Elastic
  36. redis *redis.Pool
  37. memcache *memcache.Pool
  38. merakURL string
  39. }
  40. // New init mysql db
  41. func New(c *conf.Config) (dao *Dao) {
  42. dao = &Dao{
  43. c: c,
  44. member: orm.NewMySQL(c.ORM.Member),
  45. memberRead: orm.NewMySQL(c.ORM.MemberRead),
  46. account: orm.NewMySQL(c.ORM.Account), // account-php 库
  47. fhbyophbase: hbase.NewClient(c.FHByOPHBase),
  48. fhbymidhbase: hbase.NewClient(c.FHByMidHBase),
  49. httpClient: bm.NewClient(c.HTTPClient.Read),
  50. passportClient: bm.NewClient(c.HTTPClient.Passport),
  51. bfsclient: &xhttp.Client{Timeout: time.Duration(c.FacePriBFS.Timeout)},
  52. msgURL: c.Host.Message + _msgURL,
  53. upUnameURL: c.Host.Passport + _updateUname,
  54. queryByMidsURL: c.Host.Passport + _queryByMids,
  55. merakURL: c.Host.Merak + "/",
  56. expMsgDatabus: databus.New(c.ExpMsgDatabus),
  57. es: elastic.NewElastic(c.ES),
  58. redis: redis.NewPool(c.Redis),
  59. memcache: memcache.NewPool(c.Memcache),
  60. }
  61. dao.block = block.New(c, dao.httpClient, memcache.NewPool(c.BlockMemcache), sql.NewMySQL(c.BlockMySQL))
  62. dao.initORM()
  63. return
  64. }
  65. // Close close the resource.
  66. func (d *Dao) Close() {
  67. if d.member != nil {
  68. d.member.Close()
  69. }
  70. if d.redis != nil {
  71. d.redis.Close()
  72. }
  73. }
  74. func (d *Dao) initORM() {
  75. d.member.LogMode(true)
  76. }
  77. // Ping dao ping
  78. func (d *Dao) Ping(ctx context.Context) (err error) {
  79. if err = d.member.DB().PingContext(ctx); err != nil {
  80. return
  81. }
  82. if err = d.pingRedis(ctx); err != nil {
  83. return
  84. }
  85. return
  86. }
  87. // BlockImpl is
  88. func (d *Dao) BlockImpl() *block.Dao {
  89. return d.block
  90. }