dao.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/admin/main/relation/conf"
  5. accountGRPC "go-common/app/service/main/account/api"
  6. relationGRPC "go-common/app/service/main/relation/api"
  7. "go-common/library/database/hbase.v2"
  8. "go-common/library/database/orm"
  9. "github.com/jinzhu/gorm"
  10. )
  11. // Dao dao
  12. type Dao struct {
  13. c *conf.Config
  14. ReadORM *gorm.DB
  15. accountClient accountGRPC.AccountClient
  16. relationClient relationGRPC.RelationClient
  17. hbase *hbase.Client
  18. }
  19. // New init mysql db
  20. func New(c *conf.Config) (dao *Dao) {
  21. dao = &Dao{
  22. c: c,
  23. ReadORM: orm.NewMySQL(c.ORM.Read),
  24. hbase: hbase.NewClient(c.LogHBase),
  25. }
  26. relationGRPCClient, err := relationGRPC.NewClient(c.RelationGRPC)
  27. if err != nil {
  28. panic(err)
  29. }
  30. accountGRPCClient, err := accountGRPC.NewClient(c.AccountGRPC)
  31. if err != nil {
  32. panic(err)
  33. }
  34. dao.relationClient = relationGRPCClient
  35. dao.accountClient = accountGRPCClient
  36. dao.initORM()
  37. return
  38. }
  39. func (dao *Dao) initORM() {
  40. dao.ReadORM.LogMode(true)
  41. }
  42. // Close close the resource.
  43. func (dao *Dao) Close() {
  44. dao.ReadORM.Close()
  45. }
  46. // Ping dao ping
  47. func (dao *Dao) Ping(c context.Context) error {
  48. return dao.ReadORM.DB().PingContext(c)
  49. }