es.go 867 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package db
  2. import (
  3. "log"
  4. "sfbase/core"
  5. "sfbase/elastic"
  6. "sfbase/global"
  7. )
  8. type EsConfig struct {
  9. Project struct {
  10. Addr string `mapstructure:"addr"`
  11. Index string `mapstructure:"index"`
  12. Itype string `mapstructure:"itype"`
  13. Pool int `mapstructure:"pool"`
  14. } `mapstructure:"project"`
  15. }
  16. var (
  17. Es *elastic.Elastic
  18. EsConfigs EsConfig
  19. )
  20. func InitEs() {
  21. //数据库属性定义在业务中,解析配置文件加载到结构体基本方法viper在spirit中
  22. _, err := core.Viper(global.EnvPathPrefix+"/"+global.Env+"/es.toml", &EsConfigs)
  23. if err != nil {
  24. log.Println("EsConfig:", err)
  25. return
  26. }
  27. Es = &elastic.Elastic{
  28. S_esurl: EsConfigs.Project.Addr,
  29. I_size: EsConfigs.Project.Pool,
  30. }
  31. Es.InitElasticSize()
  32. }
  33. func GetEs() *elastic.Elastic {
  34. if Es == nil {
  35. log.Printf("ERROR:Es 实例为空!请重新实例化.")
  36. }
  37. return Es
  38. }