dao.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/job/main/workflow/conf"
  5. "go-common/library/cache/redis"
  6. "go-common/library/database/elastic"
  7. "go-common/library/database/orm"
  8. bm "go-common/library/net/http/blademaster"
  9. "github.com/jinzhu/gorm"
  10. )
  11. const (
  12. _srhURL = "/x/admin/search/workflow/common"
  13. _notifyURL = "/api/notify/send.user.notify.do"
  14. )
  15. // Dao struct info of Dao.
  16. type Dao struct {
  17. c *conf.Config
  18. // orm
  19. WriteORM *gorm.DB
  20. ReadORM *gorm.DB
  21. // redis
  22. redis *redis.Pool
  23. // search
  24. httpSearch *bm.Client
  25. // url
  26. searchURL string
  27. messageURL string
  28. // es client
  29. es *elastic.Elastic
  30. }
  31. // New new a Dao and return.
  32. func New(c *conf.Config) (d *Dao) {
  33. d = &Dao{
  34. c: c,
  35. WriteORM: orm.NewMySQL(c.ORM.Write),
  36. ReadORM: orm.NewMySQL(c.ORM.Read),
  37. redis: redis.NewPool(c.Redis),
  38. httpSearch: bm.NewClient(c.HTTPSearch),
  39. searchURL: c.Host.SearchURI + _srhURL,
  40. messageURL: c.Host.MessageURI + _notifyURL,
  41. es: elastic.NewElastic(nil),
  42. }
  43. d.initORM()
  44. return
  45. }
  46. // Close close connections of ORM.
  47. func (d *Dao) Close() (err error) {
  48. if d.WriteORM != nil {
  49. d.WriteORM.Close()
  50. }
  51. if d.ReadORM != nil {
  52. d.ReadORM.Close()
  53. }
  54. return
  55. }
  56. // Ping ping health of ORM.
  57. func (d *Dao) Ping(c context.Context) (err error) {
  58. if err = d.WriteORM.DB().PingContext(c); err != nil {
  59. return
  60. }
  61. err = d.ReadORM.DB().PingContext(c)
  62. return
  63. }
  64. func (d *Dao) initORM() {
  65. d.WriteORM.LogMode(true)
  66. d.ReadORM.LogMode(true)
  67. }