dao.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package dao
  2. import (
  3. "context"
  4. account "go-common/app/service/main/account/api"
  5. archive "go-common/app/service/main/archive/api"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. "go-common/library/conf/paladin"
  9. "go-common/library/database/elastic"
  10. "go-common/library/database/orm"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/rpc/warden"
  13. "github.com/jinzhu/gorm"
  14. )
  15. // Dao is the appeal database access object
  16. type Dao struct {
  17. ReadORM *gorm.DB
  18. ORM *gorm.DB
  19. // search
  20. httpRead *bm.Client
  21. httpWrite *bm.Client
  22. // memcache
  23. mc *memcache.Pool
  24. // redis
  25. redis *redis.Pool
  26. // es
  27. es *elastic.Elastic
  28. // account-service rpc
  29. accRPC account.AccountClient
  30. // archive-service rpc
  31. arcRPC archive.ArchiveClient
  32. c *paladin.Map // application.toml can reload
  33. writeConf *bm.ClientConfig
  34. }
  35. // New will create a new appeal Dao instance
  36. func New() (d *Dao) {
  37. var (
  38. db struct {
  39. ReadORM *orm.Config
  40. ORM *orm.Config
  41. }
  42. http struct {
  43. HTTPClientRead *bm.ClientConfig
  44. HTTPClient *bm.ClientConfig
  45. Elastic *elastic.Config
  46. }
  47. grpc struct {
  48. Account *warden.ClientConfig
  49. Archive *warden.ClientConfig
  50. }
  51. mc struct {
  52. Workflow *memcache.Config
  53. }
  54. rds struct {
  55. Workflow *redis.Config
  56. }
  57. ac = new(paladin.TOML)
  58. )
  59. checkErr(paladin.Watch("application.toml", ac))
  60. checkErr(paladin.Get("mysql.toml").UnmarshalTOML(&db))
  61. checkErr(paladin.Get("http.toml").UnmarshalTOML(&http))
  62. checkErr(paladin.Get("memcache.toml").UnmarshalTOML(&mc))
  63. checkErr(paladin.Get("redis.toml").UnmarshalTOML(&rds))
  64. checkErr(paladin.Get("grpc.toml").UnmarshalTOML(&grpc))
  65. d = &Dao{
  66. c: ac,
  67. ReadORM: orm.NewMySQL(db.ReadORM),
  68. ORM: orm.NewMySQL(db.ORM),
  69. httpRead: bm.NewClient(http.HTTPClientRead),
  70. httpWrite: bm.NewClient(http.HTTPClient),
  71. // memcache
  72. mc: memcache.NewPool(mc.Workflow),
  73. // redis
  74. redis: redis.NewPool(rds.Workflow),
  75. // es
  76. //es: elastic.NewElastic(nil),
  77. es: elastic.NewElastic(http.Elastic),
  78. writeConf: http.HTTPClient,
  79. }
  80. // account-service rpc
  81. var err error
  82. if d.accRPC, err = account.NewClient(grpc.Account); err != nil {
  83. panic(err)
  84. }
  85. // archive-service rpc
  86. if d.arcRPC, err = archive.NewClient(grpc.Archive); err != nil {
  87. panic(err)
  88. }
  89. d.initORM()
  90. return
  91. }
  92. func (d *Dao) initORM() {
  93. d.ORM.LogMode(true)
  94. d.ReadORM.LogMode(true)
  95. }
  96. // Close close dao.
  97. func (d *Dao) Close() {
  98. if d.ORM != nil {
  99. d.ORM.Close()
  100. }
  101. if d.ReadORM != nil {
  102. d.ReadORM.Close()
  103. }
  104. }
  105. // Ping ping cpdb
  106. func (d *Dao) Ping(c context.Context) (err error) {
  107. if err = d.ORM.DB().PingContext(c); err != nil {
  108. return
  109. }
  110. if err = d.ReadORM.DB().PingContext(c); err != nil {
  111. return
  112. }
  113. if err = d.pingMC(c); err != nil {
  114. return
  115. }
  116. return d.pingRedis(c)
  117. }
  118. func checkErr(err error) {
  119. if err != nil {
  120. panic(err)
  121. }
  122. }