conf.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/conf"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/rpc"
  11. "go-common/library/queue/databus"
  12. "go-common/library/time"
  13. "github.com/BurntSushi/toml"
  14. )
  15. var (
  16. confPath string
  17. client *conf.Client
  18. // Conf config
  19. Conf = &Config{}
  20. )
  21. // Config .
  22. type Config struct {
  23. // Env
  24. Env string
  25. // monitor
  26. MonitorIdle time.Duration
  27. // interface XLog
  28. XLog *log.Config
  29. // BM
  30. BM *bm.ServerConfig
  31. // http client
  32. HTTPClient *bm.ClientConfig
  33. // databus
  34. StatSub *databus.Config
  35. ViewSub *databus.Config
  36. DmSub *databus.Config
  37. ReplySub *databus.Config
  38. FavSub *databus.Config
  39. CoinSub *databus.Config
  40. ShareSub *databus.Config
  41. RankSub *databus.Config
  42. LikeSub *databus.Config
  43. Memcaches []*memcache.Config
  44. // DB
  45. DB *sql.Config
  46. ClickDB *sql.Config
  47. // rpc
  48. ArchiveRPC *rpc.ClientConfig
  49. ArchiveRPC2 *rpc.ClientConfig
  50. // Monitor
  51. Monitor *Monitor
  52. }
  53. // Monitor is
  54. type Monitor struct {
  55. Users string
  56. Token string
  57. Secret string
  58. URL string
  59. }
  60. // SMS config
  61. type SMS struct {
  62. Phone string
  63. Token string
  64. }
  65. func init() {
  66. flag.StringVar(&confPath, "conf", "", "config path")
  67. }
  68. // Init init conf
  69. func Init() error {
  70. if confPath != "" {
  71. return local()
  72. }
  73. return remote()
  74. }
  75. func local() (err error) {
  76. _, err = toml.DecodeFile(confPath, &Conf)
  77. return
  78. }
  79. func remote() (err error) {
  80. if client, err = conf.New(); err != nil {
  81. return
  82. }
  83. if err = load(); err != nil {
  84. return
  85. }
  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. }