conf.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/redis"
  6. "go-common/library/conf"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. "go-common/library/log/infoc"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/rpc"
  12. "go-common/library/net/trace"
  13. "go-common/library/queue/databus"
  14. "github.com/BurntSushi/toml"
  15. )
  16. var (
  17. confPath string
  18. client *conf.Client
  19. Conf = &Config{}
  20. )
  21. type Config struct {
  22. // Env
  23. Env string
  24. LastChangeTime int64
  25. ReleaseTime int64
  26. BnjMainAid int64
  27. BnjListAids []int64
  28. // interface XLog
  29. XLog *log.Config
  30. // tracer
  31. Tracer *trace.Config
  32. // stat databus pub
  33. StatPub *databus.Config
  34. StatViewPub *databus.Config
  35. ReportMergeDatabus *databus.Config
  36. // click databus pub
  37. ClickPub *databus.Config
  38. ArchiveRPC *rpc.ClientConfig
  39. // http
  40. BM *bm.ServerConfig
  41. // redis
  42. Redis *redis.Config
  43. HTTPClient *bm.ClientConfig
  44. // cache time conf
  45. CacheConf struct {
  46. PGCReplayTime int64
  47. ArcUpCacheTime int64
  48. NewAnonymousCacheTime int64
  49. NewAnonymousBvCacheTime int64
  50. }
  51. // db
  52. DB *sql.Config
  53. // hash number
  54. HashNum int64
  55. // chan number
  56. ChanNum int64
  57. // consumer num
  58. ConsumeNum int
  59. // need Init
  60. NeedInit bool
  61. // infoc
  62. Infoc2 *infoc.Config
  63. }
  64. func init() {
  65. flag.StringVar(&confPath, "conf", "", "config path")
  66. }
  67. // Init init conf
  68. func Init() error {
  69. if confPath != "" {
  70. return local()
  71. }
  72. return remote()
  73. }
  74. func local() (err error) {
  75. _, err = toml.DecodeFile(confPath, &Conf)
  76. return
  77. }
  78. func remote() (err error) {
  79. if client, err = conf.New(); err != nil {
  80. return
  81. }
  82. if err = load(); err != nil {
  83. return
  84. }
  85. client.Watch("click-job.toml")
  86. go func() {
  87. for range client.Event() {
  88. log.Info("config reload")
  89. if load() != nil {
  90. log.Error("config reload error (%v)", err)
  91. }
  92. }
  93. }()
  94. return
  95. }
  96. func load() (err error) {
  97. var (
  98. s string
  99. ok bool
  100. tmpConf *Config
  101. )
  102. if s, ok = client.Toml2(); !ok {
  103. return errors.New("load config center error")
  104. }
  105. if _, err = toml.Decode(s, &tmpConf); err != nil {
  106. return errors.New("could not decode config")
  107. }
  108. *Conf = *tmpConf
  109. return
  110. }