conf.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/queue/databus"
  12. "go-common/library/time"
  13. "github.com/BurntSushi/toml"
  14. )
  15. // Conf global variable.
  16. var (
  17. confPath string
  18. Conf = &Config{}
  19. client *conf.Client
  20. )
  21. // Config struct of conf.
  22. type Config struct {
  23. // base
  24. // app
  25. App *bm.App
  26. // Env
  27. Env string
  28. // goroutine sleep
  29. Tick time.Duration
  30. // log
  31. Xlog *log.Config
  32. // db
  33. Mysql *sql.Config
  34. // databus
  35. DataBus *databus.Config
  36. // httpClinet
  37. HTTPClient *bm.ClientConfig
  38. // clearPath
  39. ClearPath *ClearPath
  40. // apiPath
  41. ApiPath *ApiPath
  42. // sms monitor
  43. Sms *Sms
  44. // redis
  45. // Redis *Redis
  46. RelRedis *Redis
  47. Memcache *Memcache
  48. Relation *Relation
  49. // bm
  50. BM *bm.ServerConfig
  51. }
  52. // ClearPath clear cache path
  53. type ClearPath struct {
  54. Following string
  55. Follower string
  56. Stat string
  57. }
  58. // ApiPath api path collections
  59. type ApiPath struct {
  60. FollowersNotify string
  61. }
  62. // Sms is sms monitor config.
  63. type Sms struct {
  64. Phone string
  65. Token string
  66. }
  67. type Redis struct {
  68. *redis.Config
  69. Expire time.Duration
  70. }
  71. type Memcache struct {
  72. *memcache.Config
  73. Expire time.Duration
  74. FollowerExpire time.Duration
  75. }
  76. // Relation relation related config
  77. type Relation struct {
  78. // followers unread duration
  79. // FollowersUnread time.Duration
  80. }
  81. func init() {
  82. flag.StringVar(&confPath, "conf", "", "default config path")
  83. }
  84. func Init() (err error) {
  85. if confPath != "" {
  86. return local()
  87. }
  88. return remote()
  89. }
  90. func local() (err error) {
  91. _, err = toml.DecodeFile(confPath, &Conf)
  92. return
  93. }
  94. func remote() (err error) {
  95. if client, err = conf.New(); err != nil {
  96. return
  97. }
  98. if err = load(); err != nil {
  99. return
  100. }
  101. go func() {
  102. for range client.Event() {
  103. log.Info("config reload")
  104. if load() != nil {
  105. log.Error("config reload error (%v)", err)
  106. }
  107. }
  108. }()
  109. return
  110. }
  111. func load() (err error) {
  112. var (
  113. s string
  114. ok bool
  115. tmpConf *Config
  116. )
  117. if s, ok = client.Toml2(); !ok {
  118. return errors.New("load config center error")
  119. }
  120. if _, err = toml.Decode(s, &tmpConf); err != nil {
  121. return errors.New("could not decode config")
  122. }
  123. *Conf = *tmpConf
  124. return
  125. }