conf.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/database/orm"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/rpc"
  11. "go-common/library/net/rpc/warden"
  12. "go-common/library/net/trace"
  13. "go-common/library/time"
  14. "github.com/BurntSushi/toml"
  15. )
  16. var (
  17. // ConfPath local config path
  18. ConfPath string
  19. // Conf config
  20. Conf = &Config{}
  21. client *conf.Client
  22. )
  23. // Config str
  24. type Config struct {
  25. // base
  26. // channal len
  27. ChanSize int64
  28. // log
  29. Log *log.Config
  30. // identify
  31. App *bm.App
  32. // tracer
  33. Tracer *trace.Config
  34. // tick load pgc
  35. Tick time.Duration
  36. // orm
  37. ORM *ORM
  38. // host
  39. Host *Host
  40. // Bfs
  41. Bfs *Bfs
  42. // http client
  43. HTTPClient *bm.ClientConfig
  44. // image client
  45. ImageClient *bm.ClientConfig
  46. // BM HTTPServers
  47. BM *bm.ServerConfig
  48. // budget
  49. Budget *Budget
  50. // rpc client
  51. VipRPC *rpc.ClientConfig
  52. // grpc client
  53. Account *warden.ClientConfig
  54. // shell config
  55. ShellConf *ShellConfig
  56. OtherConf *OtherConfig
  57. }
  58. // ORM is orm config
  59. type ORM struct {
  60. Allowance *sql.Config
  61. Growup *orm.Config
  62. }
  63. // Host is hosts
  64. type Host struct {
  65. Message string
  66. Common string
  67. VideoType string
  68. ColumnType string
  69. Creative string
  70. API string
  71. }
  72. // Bfs struct.
  73. type Bfs struct {
  74. Addr string
  75. Bucket string
  76. Key string
  77. Secret string
  78. MaxFileSize int
  79. }
  80. // Budget config.
  81. type Budget struct {
  82. Video *BBudget
  83. Column *BBudget
  84. Bgm *BBudget
  85. }
  86. // BBudget config.
  87. type BBudget struct {
  88. Year int64
  89. AnnualBudget int64
  90. DayBudget int64
  91. }
  92. //ShellConfig 贝壳系统配置
  93. type ShellConfig struct {
  94. CustomID string
  95. Token string
  96. PayHost string
  97. CallbackURL string
  98. }
  99. //OtherConfig 其他配置
  100. type OtherConfig struct {
  101. // true需要consume数据,false不consume数据
  102. OfflineOrderConsume bool
  103. }
  104. func init() {
  105. flag.StringVar(&ConfPath, "conf", "", "default config path")
  106. }
  107. // Init init conf
  108. func Init() (err error) {
  109. if ConfPath != "" {
  110. return local()
  111. }
  112. return remote()
  113. }
  114. func local() (err error) {
  115. _, err = toml.DecodeFile(ConfPath, &Conf)
  116. return
  117. }
  118. func remote() (err error) {
  119. if client, err = conf.New(); err != nil {
  120. return
  121. }
  122. if err = load(); err != nil {
  123. return
  124. }
  125. go func() {
  126. for range client.Event() {
  127. log.Info("config reload")
  128. if load() != nil {
  129. log.Error("config reload error (%v)", err)
  130. }
  131. }
  132. }()
  133. return
  134. }
  135. func load() (err error) {
  136. var (
  137. s string
  138. ok bool
  139. tmpConf *Config
  140. )
  141. if s, ok = client.Toml2(); !ok {
  142. return errors.New("load config center error")
  143. }
  144. if _, err = toml.Decode(s, &tmpConf); err != nil {
  145. return errors.New("could not decode config")
  146. }
  147. *Conf = *tmpConf
  148. return
  149. }