conf.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. xsql "go-common/library/database/sql"
  9. ecode "go-common/library/ecode/tip"
  10. "go-common/library/log"
  11. xhttp "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/trace"
  15. xtime "go-common/library/time"
  16. "github.com/BurntSushi/toml"
  17. )
  18. // Conf info.
  19. var (
  20. confPath string
  21. client *conf.Client
  22. // Conf config
  23. Conf = &Config{}
  24. )
  25. // Config struct.
  26. type Config struct {
  27. Ecode *ecode.Config
  28. Verify *verify.Config
  29. MySQL *xsql.Config
  30. Log *log.Config
  31. HTTPServer *xhttp.ServerConfig
  32. HTTPClient *xhttp.ClientConfig
  33. FilterRPC *rpc.ClientConfig
  34. Tracer *trace.Config
  35. Redis *rds
  36. Memcache *mc
  37. Wechat *wechat
  38. Cfg *cfg
  39. BizID *bizid
  40. }
  41. type rds struct {
  42. *redis.Config
  43. LimitDayExpire xtime.Duration
  44. }
  45. type mc struct {
  46. *memcache.Config
  47. UUIDExpire xtime.Duration
  48. CDExpire xtime.Duration
  49. }
  50. type wechat struct {
  51. Token string
  52. Secret string
  53. Username string
  54. }
  55. type bizid struct {
  56. Live int
  57. Archive int
  58. }
  59. type cfg struct {
  60. LoadTaskInteval xtime.Duration
  61. LoadBusinessInteval xtime.Duration
  62. LoadSettingsInteval xtime.Duration
  63. NASPath string
  64. LimitUserPerDay int
  65. HandleTaskGoroutines int
  66. HandleMidGoroutines int
  67. CacheSize int
  68. }
  69. func init() {
  70. flag.StringVar(&confPath, "conf", "", "default config path")
  71. }
  72. // Init init conf
  73. func Init() error {
  74. if confPath != "" {
  75. return local()
  76. }
  77. return remote()
  78. }
  79. func local() (err error) {
  80. _, err = toml.DecodeFile(confPath, &Conf)
  81. return
  82. }
  83. func remote() (err error) {
  84. if client, err = conf.New(); err != nil {
  85. return
  86. }
  87. err = load()
  88. return
  89. }
  90. func load() (err error) {
  91. var (
  92. s string
  93. ok bool
  94. tmpConf *Config
  95. )
  96. if s, ok = client.Toml2(); !ok {
  97. return errors.New("load config center error")
  98. }
  99. if _, err = toml.Decode(s, &tmpConf); err != nil {
  100. return errors.New("could not decode config")
  101. }
  102. *Conf = *tmpConf
  103. return
  104. }