dao.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package dao
  2. import (
  3. "context"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "crypto/tls"
  7. "go-common/app/service/main/account-recovery/conf"
  8. account "go-common/app/service/main/account/api"
  9. location "go-common/app/service/main/location/rpc/client"
  10. member "go-common/app/service/main/member/api"
  11. "go-common/library/cache/redis"
  12. "go-common/library/database/elastic"
  13. xsql "go-common/library/database/sql"
  14. bm "go-common/library/net/http/blademaster"
  15. "gopkg.in/gomail.v2"
  16. )
  17. // Dao dao
  18. type Dao struct {
  19. c *conf.Config
  20. redis *redis.Pool
  21. db *xsql.DB
  22. // httpClient
  23. httpClient *bm.Client
  24. // email
  25. email *gomail.Dialer
  26. es *elastic.Elastic
  27. // rpc
  28. locRPC *location.Service
  29. // grpc
  30. memberClient member.MemberClient
  31. accountClient account.AccountClient
  32. hashSalt []byte
  33. AESBlock cipher.Block
  34. }
  35. // New init mysql db
  36. func New(c *conf.Config) (dao *Dao) {
  37. dao = &Dao{
  38. c: c,
  39. redis: redis.NewPool(c.Redis),
  40. db: xsql.NewMySQL(c.MySQL),
  41. // httpClient
  42. httpClient: bm.NewClient(c.HTTPClientConfig),
  43. email: gomail.NewDialer(c.MailConf.Host, c.MailConf.Port, c.MailConf.Username, c.MailConf.Password),
  44. es: elastic.NewElastic(c.Elastic),
  45. locRPC: location.New(c.LocationRPC),
  46. hashSalt: []byte(c.AESEncode.Salt),
  47. }
  48. dao.email.TLSConfig = &tls.Config{
  49. InsecureSkipVerify: true,
  50. }
  51. dao.AESBlock, _ = aes.NewCipher([]byte(c.AESEncode.AesKey))
  52. var err error
  53. if dao.memberClient, err = member.NewClient(c.MemberGRPC); err != nil {
  54. panic(err)
  55. }
  56. if dao.accountClient, err = account.NewClient(c.AccountGRPC); err != nil {
  57. panic(err)
  58. }
  59. return
  60. }
  61. // Close close the resource.
  62. func (d *Dao) Close() {
  63. d.redis.Close()
  64. d.db.Close()
  65. }
  66. // Ping dao ping
  67. func (d *Dao) Ping(c context.Context) (err error) {
  68. if err = d.db.Ping(c); err != nil {
  69. return
  70. }
  71. if err = d.PingRedis(c); err != nil {
  72. return
  73. }
  74. // TODO: if you need use mc,redis, please add
  75. return
  76. }