conf.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. xtime "go-common/library/time"
  10. "github.com/BurntSushi/toml"
  11. )
  12. // Conf global variable.
  13. var (
  14. Conf = &Config{}
  15. client *conf.Client
  16. confPath string
  17. )
  18. // Config struct of conf.
  19. type Config struct {
  20. // base
  21. // Xlog xlog conf
  22. Xlog *log.Config
  23. // DB multiple db conf
  24. DB *DB
  25. // HTTPClient httpClient conf
  26. HTTPClient *bm.ClientConfig
  27. //ES
  28. ElasticSearch *ElasticSearch
  29. ElasticSearchUgc *ElasticSearch
  30. //comment
  31. Comment *Comment
  32. BM *HTTPServers
  33. //berserker
  34. Berserker *Berserker
  35. }
  36. // HTTPServers bm inner config
  37. type HTTPServers struct {
  38. Inner *bm.ServerConfig
  39. }
  40. // Berserker api conf
  41. type Berserker struct {
  42. Appkey string
  43. Secret string
  44. URL string
  45. }
  46. // ElasticSearch elasticSearch.
  47. type ElasticSearch struct {
  48. Addr []string
  49. Check xtime.Duration
  50. Timeout string
  51. }
  52. // Comment config with url and type
  53. type Comment struct {
  54. URL string
  55. Type int
  56. }
  57. // DB config
  58. type DB struct {
  59. TicketDB *sql.Config
  60. }
  61. func init() {
  62. flag.StringVar(&confPath, "conf", "", "default config path")
  63. }
  64. // Init init conf.
  65. func Init() (err error) {
  66. if confPath != "" {
  67. return local()
  68. }
  69. return remote()
  70. }
  71. func local() (err error) {
  72. _, err = toml.DecodeFile(confPath, &Conf)
  73. return
  74. }
  75. func remote() (err error) {
  76. if client, err = conf.New(); err != nil {
  77. return
  78. }
  79. if err = load(); err != nil {
  80. return
  81. }
  82. go func() {
  83. for range client.Event() {
  84. log.Info("config reload")
  85. if load() != nil {
  86. log.Error("config reload error (%v)", err)
  87. }
  88. }
  89. }()
  90. return
  91. }
  92. func load() (err error) {
  93. var (
  94. s string
  95. ok bool
  96. tmpConf *Config
  97. )
  98. if s, ok = client.Value("open-market-job.toml"); !ok {
  99. return errors.New("load config center error")
  100. }
  101. if _, err = toml.Decode(s, &tmpConf); err != nil {
  102. return errors.New("could not decode config")
  103. }
  104. *Conf = *tmpConf
  105. return
  106. }