conf.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/sql"
  8. xlog "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/rpc/warden"
  11. "go-common/library/net/trace"
  12. xtime "go-common/library/time"
  13. "github.com/BurntSushi/toml"
  14. )
  15. // Config .
  16. type Config struct {
  17. Log *xlog.Config
  18. Tracer *trace.Config
  19. HTTPServer *bm.ServerConfig
  20. HTTPClient *bm.ClientConfig
  21. AppresClient *warden.ClientConfig
  22. MySQL *sql.Config
  23. Cfg *Cfg // push cfg.
  24. Redis *redis.Config
  25. Bfs *Bfs
  26. }
  27. // Cfg .
  28. type Cfg struct {
  29. Push *PushCfg
  30. Diff *DiffCfg
  31. Grpc *GrpcCfg
  32. }
  33. // DiffCfg represents the diff calc config
  34. type DiffCfg struct {
  35. FreDiff xtime.Duration // diff calculation frequency
  36. Folder string
  37. Retry string
  38. }
  39. // Bfs represents the bfs config
  40. type Bfs struct {
  41. Key string
  42. Secret string
  43. Host string
  44. Timeout int
  45. OldURL string
  46. NewURL string
  47. }
  48. // PushCfg def.
  49. type PushCfg struct {
  50. QPS int // qps limit
  51. Operation int // operation number
  52. URL string // push url
  53. Timeout int64 // timeout
  54. Fre xtime.Duration // frequency
  55. Pause xtime.Duration // pause between call app-resource and broadcast
  56. }
  57. // GrpcCfg def.
  58. type GrpcCfg struct {
  59. ApiAppID string
  60. Method string
  61. }
  62. var (
  63. confPath string
  64. client *conf.Client
  65. // Conf config
  66. Conf = &Config{}
  67. )
  68. func init() {
  69. flag.StringVar(&confPath, "conf", "", "config path")
  70. }
  71. // Init .
  72. func Init() (err error) {
  73. if confPath != "" {
  74. return local()
  75. }
  76. return remote()
  77. }
  78. func local() (err error) {
  79. _, err = toml.DecodeFile(confPath, &Conf)
  80. return
  81. }
  82. func remote() (err error) {
  83. if client, err = conf.New(); err != nil {
  84. return
  85. }
  86. err = load()
  87. return
  88. }
  89. func load() (err error) {
  90. var (
  91. s string
  92. ok bool
  93. tmpConf *Config
  94. )
  95. if s, ok = client.Toml2(); !ok {
  96. return errors.New("load config center error")
  97. }
  98. if _, err = toml.Decode(s, &tmpConf); err != nil {
  99. return errors.New("could not decode config")
  100. }
  101. *Conf = *tmpConf
  102. return
  103. }