conf.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/cache/redis"
  7. "go-common/library/conf"
  8. "go-common/library/database/sql"
  9. ecode "go-common/library/ecode/tip"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/http/blademaster/middleware/verify"
  13. "go-common/library/net/rpc"
  14. "go-common/library/net/rpc/warden"
  15. "go-common/library/net/trace"
  16. "go-common/library/queue/databus"
  17. xtime "go-common/library/time"
  18. "github.com/BurntSushi/toml"
  19. )
  20. // global var
  21. var (
  22. confPath string
  23. client *conf.Client
  24. // Conf config
  25. Conf = &Config{}
  26. )
  27. // Config config set
  28. type Config struct {
  29. Env string
  30. Ecode *ecode.Config
  31. Log *log.Config
  32. HTTPServer *bm.ServerConfig
  33. HTTPClient *bm.ClientConfig
  34. RPCServer *rpc.ServerConfig
  35. GRPC *warden.ServerConfig
  36. FilterRPC *rpc.ClientConfig
  37. Tracer *trace.Config
  38. Verify *verify.Config
  39. App *bm.App
  40. Redis *rds
  41. Memcache *mc
  42. MySQL *sql.Config
  43. ReportPub *databus.Config
  44. CallbackPub *databus.Config
  45. Android *android
  46. Apns *apns
  47. Push *push
  48. }
  49. // mc config
  50. type mc struct {
  51. *memcache.Config
  52. SettingExpire xtime.Duration
  53. ReportExpire xtime.Duration
  54. UUIDExpire xtime.Duration
  55. }
  56. type rds struct {
  57. *redis.Config
  58. TokenExpire xtime.Duration
  59. LaterExpire xtime.Duration
  60. MidsExpire xtime.Duration
  61. }
  62. type android struct {
  63. PoolSize int
  64. Timeout xtime.Duration
  65. PushHuaweiPart int
  66. MiUseVip int
  67. }
  68. type apns struct {
  69. PoolSize int
  70. Proxy int
  71. ProxySocket string
  72. Timeout xtime.Duration
  73. Deadline xtime.Duration
  74. }
  75. type push struct {
  76. PickUpTask bool
  77. LoadBusinessInteval xtime.Duration
  78. LoadTaskInteval xtime.Duration
  79. UpdateTaskProgressInteval xtime.Duration
  80. PushChanSizeAPNS, PushGoroutinesAPNS int
  81. PushChanSizeMi, PushGoroutinesMi int
  82. PushChanSizeHuawei, PushGoroutinesHuawei int
  83. PushChanSizeOppo, PushGoroutinesOppo int
  84. PushChanSizeJpush, PushGoroutinesJpush int
  85. PushChanSizeFCM, PushGoroutinesFCM int
  86. PassThrough int
  87. RetryTimes int
  88. PushPartInterval xtime.Duration
  89. PushPartChanSize int
  90. PushPartSize int
  91. CallbackSize, CallbackChanLen int
  92. UpimgURL string
  93. UpimgMaxSize int64
  94. UpdateTaskProgressProc int
  95. }
  96. func init() {
  97. flag.StringVar(&confPath, "conf", "", "default config path")
  98. }
  99. // Init init conf
  100. func Init() error {
  101. if confPath != "" {
  102. return local()
  103. }
  104. return remote()
  105. }
  106. func local() (err error) {
  107. _, err = toml.DecodeFile(confPath, &Conf)
  108. return
  109. }
  110. func remote() (err error) {
  111. if client, err = conf.New(); err != nil {
  112. return
  113. }
  114. err = load()
  115. return
  116. }
  117. func load() (err error) {
  118. var (
  119. s string
  120. ok bool
  121. tmpConf *Config
  122. )
  123. if s, ok = client.Toml2(); !ok {
  124. return errors.New("load config center error")
  125. }
  126. if _, err = toml.Decode(s, &tmpConf); err != nil {
  127. return errors.New("could not decode config")
  128. }
  129. *Conf = *tmpConf
  130. return
  131. }