conf.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/rpc"
  9. "go-common/library/queue/databus"
  10. "github.com/BurntSushi/toml"
  11. )
  12. var (
  13. confPath string
  14. client *conf.Client
  15. Conf = &Config{}
  16. )
  17. type Config struct {
  18. // Env
  19. Env string
  20. // interface XLog
  21. XLog *log.Config
  22. // databus
  23. ArchiveSub *databus.Config
  24. // BM
  25. BM *bm.ServerConfig
  26. // rpc
  27. FeedRPC *rpc.ClientConfig
  28. // httpClinet
  29. HTTPClient *bm.ClientConfig
  30. SMS *SMS
  31. }
  32. // SMS config
  33. type SMS struct {
  34. Phone string
  35. Token string
  36. }
  37. func init() {
  38. flag.StringVar(&confPath, "conf", "", "config path")
  39. }
  40. // Init init conf
  41. func Init() error {
  42. if confPath != "" {
  43. return local()
  44. }
  45. return remote()
  46. }
  47. func local() (err error) {
  48. _, err = toml.DecodeFile(confPath, &Conf)
  49. return
  50. }
  51. func remote() (err error) {
  52. if client, err = conf.New(); err != nil {
  53. return
  54. }
  55. if err = load(); err != nil {
  56. return
  57. }
  58. go func() {
  59. for range client.Event() {
  60. log.Info("config reload")
  61. if load() != nil {
  62. log.Error("config reload error (%v)", err)
  63. }
  64. }
  65. }()
  66. return
  67. }
  68. func load() (err error) {
  69. var (
  70. s string
  71. ok bool
  72. tmpConf *Config
  73. )
  74. if s, ok = client.Toml2(); !ok {
  75. return errors.New("load config center error")
  76. }
  77. if _, err = toml.Decode(s, &tmpConf); err != nil {
  78. return errors.New("could not decode config")
  79. }
  80. *Conf = *tmpConf
  81. return
  82. }