conf.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  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. "go-common/library/queue/databus"
  13. xtime "go-common/library/time"
  14. "github.com/BurntSushi/toml"
  15. )
  16. // Config .
  17. type Config struct {
  18. Env string
  19. Log *xlog.Config
  20. Tracer *trace.Config
  21. PushRPC *warden.ClientConfig
  22. HTTPServer *bm.ServerConfig
  23. HTTPClient *bm.ClientConfig
  24. DpClient *bm.ClientConfig
  25. ReportSub *databus.Config
  26. CallbackSub *databus.Config
  27. MySQL *sql.Config
  28. Memcache *mc
  29. Wechat *wechat
  30. Job *job
  31. }
  32. // mc config
  33. type mc struct {
  34. *memcache.Config
  35. }
  36. type wechat struct {
  37. Token string
  38. Secret string
  39. Username string
  40. }
  41. type job struct {
  42. ReportTicker xtime.Duration
  43. DelInvalidReportInterval xtime.Duration
  44. LoadTaskInteval xtime.Duration
  45. PullResultIntervalHour int
  46. DelCallbackInterval int
  47. DelTaskInterval int
  48. SyncReportCacheWeek int
  49. SyncReportCacheHour int
  50. ReportShard int
  51. CallbackShard int
  52. PretreatmentTaskShard int
  53. TaskGoroutines int
  54. LimitPerTask int
  55. PushPartSize int
  56. PushPartChanSize int
  57. MountDir string
  58. PretreatTask bool
  59. DpPollingTime xtime.Duration
  60. }
  61. var (
  62. confPath string
  63. client *conf.Client
  64. // Conf config
  65. Conf = &Config{}
  66. )
  67. func init() {
  68. flag.StringVar(&confPath, "conf", "", "config path")
  69. }
  70. // Init .
  71. func Init() (err error) {
  72. if confPath != "" {
  73. return local()
  74. }
  75. return remote()
  76. }
  77. func local() (err error) {
  78. _, err = toml.DecodeFile(confPath, &Conf)
  79. return
  80. }
  81. func remote() (err error) {
  82. if client, err = conf.New(); err != nil {
  83. return
  84. }
  85. err = load()
  86. return
  87. }
  88. func load() (err error) {
  89. var (
  90. s string
  91. ok bool
  92. tmpConf *Config
  93. )
  94. if s, ok = client.Toml2(); !ok {
  95. return errors.New("load config center error")
  96. }
  97. if _, err = toml.Decode(s, &tmpConf); err != nil {
  98. return errors.New("could not decode config")
  99. }
  100. *Conf = *tmpConf
  101. return
  102. }