conf.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/log"
  7. "go-common/library/net/http/blademaster"
  8. "go-common/library/net/http/blademaster/middleware/verify"
  9. "go-common/library/net/rpc"
  10. "go-common/library/net/trace"
  11. xtime "go-common/library/time"
  12. "go-common/library/database/hbase.v2"
  13. "github.com/BurntSushi/toml"
  14. )
  15. // Conf global variable.
  16. var (
  17. Conf = &Config{}
  18. client *conf.Client
  19. confPath string
  20. )
  21. // HBaseConfig ...
  22. type HBaseConfig struct {
  23. *hbase.Config
  24. WriteTimeout xtime.Duration
  25. ReadTimeout xtime.Duration
  26. }
  27. // Config struct of conf.
  28. type Config struct {
  29. // base
  30. // log
  31. Xlog *log.Config
  32. // tracer
  33. Tracer *trace.Config
  34. // identify
  35. Identify *verify.Config
  36. // BM
  37. BM *blademaster.ServerConfig
  38. // Switch switch
  39. Switch *Switch
  40. // RPCServer rpc server2
  41. RPCServer *rpc.ServerConfig
  42. // HBase
  43. HBase *HBase
  44. }
  45. // Switch switch.
  46. type Switch struct {
  47. LoginLogHBase bool
  48. RPC bool
  49. }
  50. // HBase multi hbase.
  51. type HBase struct {
  52. FaceApply *HBaseConfig
  53. LoginLog *HBaseConfig
  54. PwdLog *HBaseConfig
  55. }
  56. func local() (err error) {
  57. _, err = toml.DecodeFile(confPath, &Conf)
  58. return
  59. }
  60. func remote() (err error) {
  61. if client, err = conf.New(); err != nil {
  62. return
  63. }
  64. if err = load(); err != nil {
  65. return
  66. }
  67. go func() {
  68. for range client.Event() {
  69. log.Info("config reload")
  70. if load() != nil {
  71. log.Error("config reload error (%v)", err)
  72. }
  73. }
  74. }()
  75. return
  76. }
  77. func load() (err error) {
  78. var (
  79. s string
  80. ok bool
  81. tmpConf *Config
  82. )
  83. if s, ok = client.Toml2(); !ok {
  84. return errors.New("load config center error")
  85. }
  86. if _, err = toml.Decode(s, &tmpConf); err != nil {
  87. return errors.New("could not decode config")
  88. }
  89. *Conf = *tmpConf
  90. return
  91. }
  92. func init() {
  93. flag.StringVar(&confPath, "conf", "", "default config path")
  94. }
  95. // Init int config
  96. func Init() error {
  97. if confPath != "" {
  98. return local()
  99. }
  100. return remote()
  101. }