conf.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/app/service/live/resource/sdk"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/conf"
  8. "go-common/library/database/hbase.v2"
  9. ecode "go-common/library/ecode/tip"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/http/blademaster/middleware/verify"
  13. "go-common/library/net/trace"
  14. "github.com/BurntSushi/toml"
  15. "go-common/library/net/rpc/warden"
  16. )
  17. var (
  18. confPath string
  19. client *conf.Client
  20. // Conf config
  21. Conf = &Config{}
  22. )
  23. // Config .
  24. type Config struct {
  25. Log *log.Config
  26. BM *bm.ServerConfig
  27. Verify *verify.Config
  28. Tracer *trace.Config
  29. Memcache *memcache.Config
  30. Ecode *ecode.Config
  31. Titan *titansSdk.Config
  32. BMClient *bm.ClientConfig
  33. HBase *hbase.Config
  34. Warden *warden.ClientConfig
  35. }
  36. func init() {
  37. flag.StringVar(&confPath, "conf", "", "default config path")
  38. }
  39. // Init init conf
  40. func Init() error {
  41. if confPath != "" {
  42. return local()
  43. }
  44. return remote()
  45. }
  46. func local() (err error) {
  47. _, err = toml.DecodeFile(confPath, &Conf)
  48. return
  49. }
  50. func remote() (err error) {
  51. if client, err = conf.New(); err != nil {
  52. return
  53. }
  54. if err = load(); err != nil {
  55. return
  56. }
  57. go func() {
  58. for range client.Event() {
  59. log.Info("config reload")
  60. if load() != nil {
  61. log.Error("config reload error (%v)", err)
  62. }
  63. }
  64. }()
  65. return
  66. }
  67. func load() (err error) {
  68. var (
  69. s string
  70. ok bool
  71. tmpConf *Config
  72. )
  73. if s, ok = client.Toml2(); !ok {
  74. return errors.New("load config center error")
  75. }
  76. if _, err = toml.Decode(s, &tmpConf); err != nil {
  77. return errors.New("could not decode config")
  78. }
  79. *Conf = *tmpConf
  80. return
  81. }