conf.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/redis"
  6. "go-common/library/conf"
  7. "go-common/library/database/orm"
  8. ecode "go-common/library/ecode/tip"
  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"
  13. "go-common/library/net/rpc/warden"
  14. "go-common/library/net/trace"
  15. "go-common/library/queue/databus"
  16. "go-common/library/time"
  17. "github.com/BurntSushi/toml"
  18. )
  19. // ConfPath str
  20. var (
  21. ConfPath string
  22. Conf = &Config{}
  23. client *conf.Client
  24. )
  25. // Config str
  26. type Config struct {
  27. // base
  28. // channal len
  29. ChanSize int64
  30. // log
  31. Log *log.Config
  32. // identify
  33. App *bm.App
  34. // auth
  35. Auth *permit.Config
  36. // tracer
  37. Tracer *trace.Config
  38. // tick load pgc
  39. Tick time.Duration
  40. // orm
  41. ORM *orm.Config
  42. //orm
  43. ORMArchive *orm.Config
  44. // redis
  45. Redis *Redis
  46. // HTTPClient client
  47. HTTPClient *bm.ClientConfig
  48. // rpc
  49. ArchiveRPC *rpc.ClientConfig
  50. ArticleRPC *rpc.ClientConfig
  51. // http
  52. BM *bm.ServerConfig
  53. // bfs
  54. Bfs *Bfs
  55. //ManagerReport
  56. ManagerReport *databus.Config
  57. //Ecode
  58. Ecode *ecode.Config
  59. // host
  60. Host *Host
  61. // grpc
  62. AccClient *warden.ClientConfig
  63. }
  64. // Host host config .
  65. type Host struct {
  66. Msg string
  67. }
  68. // Bfs reprensents the bfs config
  69. type Bfs struct {
  70. Key string
  71. Secret string
  72. Host string
  73. Timeout int
  74. MaxFileSize int
  75. }
  76. // Redis str
  77. type Redis struct {
  78. Track *struct {
  79. *redis.Config
  80. Expire time.Duration
  81. }
  82. }
  83. // HTTPClient str
  84. type HTTPClient struct {
  85. Read *bm.ClientConfig
  86. Write *bm.ClientConfig
  87. }
  88. func init() {
  89. flag.StringVar(&ConfPath, "conf", "", "default config path")
  90. }
  91. // Init fn
  92. func Init() (err error) {
  93. if ConfPath != "" {
  94. return local()
  95. }
  96. return remote()
  97. }
  98. func local() (err error) {
  99. _, err = toml.DecodeFile(ConfPath, &Conf)
  100. return
  101. }
  102. func remote() (err error) {
  103. if client, err = conf.New(); err != nil {
  104. return
  105. }
  106. if err = load(); err != nil {
  107. return
  108. }
  109. go func() {
  110. for range client.Event() {
  111. log.Info("config reload")
  112. if load() != nil {
  113. log.Error("config reload error (%v)", err)
  114. }
  115. }
  116. }()
  117. return
  118. }
  119. func load() (err error) {
  120. var (
  121. s string
  122. ok bool
  123. tmpConf *Config
  124. )
  125. if s, ok = client.Toml2(); !ok {
  126. return errors.New("load config center error")
  127. }
  128. if _, err = toml.Decode(s, &tmpConf); err != nil {
  129. return errors.New("could not decode config")
  130. }
  131. *Conf = *tmpConf
  132. return
  133. }