conf.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package conf
  2. import (
  3. "flag"
  4. "fmt"
  5. "go-common/library/cache/redis"
  6. "go-common/library/conf"
  7. "go-common/library/database/hbase.v2"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/http/blademaster/middleware/permit"
  12. "go-common/library/net/rpc/warden"
  13. "go-common/library/queue/databus"
  14. "go-common/library/time"
  15. "github.com/BurntSushi/toml"
  16. )
  17. //Config config.
  18. type Config struct {
  19. Xlog *log.Config
  20. ManagerReport *databus.Config
  21. HTTPClient *bm.ClientConfig
  22. BM *bm.ServerConfig
  23. DB *db
  24. Auth *permit.Config
  25. Host host
  26. HBase *HBaseConfig
  27. // redis
  28. Redis *Redis
  29. GRPC *GRPC
  30. }
  31. // HBaseConfig extra hbase config
  32. type HBaseConfig struct {
  33. *hbase.Config
  34. ReadTimeout time.Duration
  35. WriteTimeout time.Duration
  36. }
  37. //GRPC .
  38. type GRPC struct {
  39. AccRPC *warden.ClientConfig
  40. UpsRPC *warden.ClientConfig
  41. }
  42. // Redis .
  43. type Redis struct {
  44. Weight *struct {
  45. *redis.Config
  46. Expire time.Duration
  47. }
  48. }
  49. type host struct {
  50. API string
  51. Manager string
  52. Search string
  53. }
  54. type db struct {
  55. Archive *sql.Config
  56. ArchiveRead *sql.Config
  57. Manager *sql.Config
  58. }
  59. //common + xlog(agent) + trace(better) + http + perf(web代码性能分析) + os.signal监听(stop/reload服务)
  60. var (
  61. confPath string
  62. client *conf.Client
  63. Conf = &Config{}
  64. )
  65. func init() {
  66. flag.StringVar(&confPath, "conf", "", "default config path")
  67. }
  68. //Init config init.
  69. func Init() error {
  70. if confPath != "" {
  71. return local()
  72. }
  73. return remote()
  74. }
  75. func local() (err error) {
  76. tmpConf := &Config{}
  77. if _, err = toml.DecodeFile(confPath, tmpConf); err == nil {
  78. Conf = tmpConf
  79. }
  80. return
  81. }
  82. func remote() (err error) {
  83. if client, err = conf.New(); err != nil {
  84. return
  85. }
  86. if err = load(); err != nil {
  87. return
  88. }
  89. go func() {
  90. for range client.Event() {
  91. if err = load(); err != nil {
  92. log.Error("config reload error (%v)", err)
  93. }
  94. }
  95. }()
  96. return
  97. }
  98. func load() (err error) {
  99. var (
  100. ok bool
  101. tmpConf = &Config{}
  102. )
  103. if confPath, ok = client.Toml2(); !ok {
  104. err = fmt.Errorf("config load error")
  105. return
  106. }
  107. if _, err = toml.Decode(confPath, tmpConf); err != nil {
  108. err = fmt.Errorf("couldn't decode config, error (%v)", err)
  109. return
  110. }
  111. Conf = tmpConf
  112. return
  113. }