dao.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/openplatform/open-market/conf"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. elastic "gopkg.in/olivere/elastic.v5"
  10. )
  11. //Dao struct
  12. type Dao struct {
  13. c *conf.Config
  14. // http client
  15. client *bm.Client
  16. // db
  17. ticketDB *sql.DB
  18. //es
  19. es *elastic.Client
  20. esUgc *elastic.Client
  21. }
  22. // New new a Dao and return.
  23. func New(c *conf.Config) (d *Dao) {
  24. var (
  25. err error
  26. es *elastic.Client
  27. esUgc *elastic.Client
  28. )
  29. d = &Dao{
  30. c: c,
  31. client: bm.NewClient(c.HTTPClient),
  32. ticketDB: sql.NewMySQL(c.DB.TicketDB),
  33. }
  34. es, err = elastic.NewClient(
  35. elastic.SetURL(c.ElasticSearch.Addr...),
  36. elastic.SetSniff(false),
  37. elastic.SetHealthcheckInterval(time.Duration(c.ElasticSearch.Check)),
  38. elastic.SetErrorLog(&elog{}),
  39. elastic.SetInfoLog(&ilog{}),
  40. )
  41. if err != nil {
  42. panic(err)
  43. }
  44. esUgc, err = elastic.NewClient(
  45. elastic.SetURL(c.ElasticSearchUgc.Addr...),
  46. elastic.SetSniff(false),
  47. elastic.SetHealthcheckInterval(time.Duration(c.ElasticSearch.Check)),
  48. elastic.SetErrorLog(&elog{}),
  49. elastic.SetInfoLog(&ilog{}),
  50. )
  51. if err != nil {
  52. panic(err)
  53. }
  54. d.es = es
  55. d.esUgc = esUgc
  56. return d
  57. }
  58. // Ping ping health.
  59. func (d *Dao) Ping(c context.Context) (err error) {
  60. return d.ticketDB.Ping(c)
  61. }
  62. // Close close.
  63. func (d *Dao) Close() (err error) {
  64. if err = d.ticketDB.Close(); err != nil {
  65. log.Error("dao.ticketDB.Close() error(%v)", err)
  66. }
  67. return
  68. }
  69. type ilog struct{}
  70. type elog struct{}
  71. // Printf printf.
  72. func (l *ilog) Printf(format string, v ...interface{}) {
  73. log.Info(format, v...)
  74. }
  75. // Printf printf.
  76. func (l *elog) Printf(format string, v ...interface{}) {
  77. log.Error(format, v...)
  78. }