conf.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/conf"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/http/blademaster/middleware/verify"
  10. "go-common/library/net/rpc"
  11. "go-common/library/net/trace"
  12. "go-common/library/time"
  13. "github.com/BurntSushi/toml"
  14. )
  15. // global var
  16. var (
  17. confPath string
  18. client *conf.Client
  19. // Conf config
  20. Conf = &Config{}
  21. )
  22. // Config .
  23. type Config struct {
  24. // elk
  25. Log *log.Config
  26. // Verify
  27. Verify *verify.Config
  28. // app
  29. App *bm.App
  30. // http client
  31. HTTPClient httpClient
  32. // rpc
  33. ArchiveRPC *rpc.ClientConfig
  34. // rpc server
  35. RPCServer *rpc.ServerConfig
  36. // tracer
  37. Tracer *trace.Config
  38. // mc
  39. Memcache *Memcache
  40. // Rule
  41. Rule *Rule
  42. // Host
  43. Host *Host
  44. // HTTPServer
  45. HTTPServer *bm.ServerConfig
  46. }
  47. // Host hosts.
  48. type Host struct {
  49. BigDataURI string
  50. LiveURI string
  51. APIURI string
  52. }
  53. // Rule config.
  54. type Rule struct {
  55. // region tick.
  56. TickRegion time.Duration
  57. // tag tick.
  58. TickTag time.Duration
  59. // default num of dynamic archives.
  60. NumArcs int
  61. //default num of index dynamic archives.
  62. NumIndexArcs int
  63. //min region count.
  64. MinRegionCount int
  65. }
  66. type httpClient struct {
  67. Read *bm.ClientConfig
  68. }
  69. // Memcache memcache
  70. type Memcache struct {
  71. *memcache.Config
  72. Expire time.Duration
  73. }
  74. func init() {
  75. flag.StringVar(&confPath, "conf", "", "config path")
  76. }
  77. // Init init conf
  78. func Init() (err error) {
  79. if confPath != "" {
  80. return local()
  81. }
  82. return remote()
  83. }
  84. func local() (err error) {
  85. _, err = toml.DecodeFile(confPath, &Conf)
  86. return
  87. }
  88. func remote() (err error) {
  89. if client, err = conf.New(); err != nil {
  90. return
  91. }
  92. if err = load(); err != nil {
  93. return
  94. }
  95. go func() {
  96. for range client.Event() {
  97. log.Info("config reload")
  98. if load() != nil {
  99. log.Error("config reload error (%v)", err)
  100. }
  101. }
  102. }()
  103. return
  104. }
  105. func load() (err error) {
  106. var (
  107. s string
  108. ok bool
  109. tmpConf *Config
  110. )
  111. if s, ok = client.Toml2(); !ok {
  112. return errors.New("load config center error")
  113. }
  114. if _, err = toml.Decode(s, &tmpConf); err != nil {
  115. return errors.New("could not decode config")
  116. }
  117. *Conf = *tmpConf
  118. return
  119. }