conf.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/trace"
  14. "go-common/library/queue/databus"
  15. xtime "go-common/library/time"
  16. "github.com/BurntSushi/toml"
  17. )
  18. var (
  19. confPath string
  20. client *conf.Client
  21. // Conf config
  22. Conf = &Config{}
  23. )
  24. // Config .
  25. type Config struct {
  26. Log *log.Config
  27. BM *bm.ServerConfig
  28. Verify *verify.Config
  29. Tracer *trace.Config
  30. Redis *redis.Config
  31. Memcache *memcache.Config
  32. MySQL *sql.Config
  33. Ecode *ecode.Config
  34. RelationBinLog *databus.Config
  35. HTTPClient *bm.ClientConfig
  36. CacheTTL *CacheTTL
  37. }
  38. // ClearPath clear cache path
  39. // type ClearPath struct {
  40. // Following string
  41. // Follower string
  42. // Stat string
  43. // }
  44. // CacheTTL is
  45. type CacheTTL struct {
  46. RelationTTL xtime.Duration
  47. }
  48. func init() {
  49. flag.StringVar(&confPath, "conf", "", "default config path")
  50. }
  51. // Init init conf
  52. func Init() error {
  53. if confPath != "" {
  54. return local()
  55. }
  56. return remote()
  57. }
  58. func local() (err error) {
  59. _, err = toml.DecodeFile(confPath, &Conf)
  60. return
  61. }
  62. func remote() (err error) {
  63. if client, err = conf.New(); err != nil {
  64. return
  65. }
  66. if err = load(); err != nil {
  67. return
  68. }
  69. go func() {
  70. for range client.Event() {
  71. log.Info("config reload")
  72. if load() != nil {
  73. log.Error("config reload error (%v)", err)
  74. }
  75. }
  76. }()
  77. return
  78. }
  79. func load() (err error) {
  80. var (
  81. s string
  82. ok bool
  83. tmpConf *Config
  84. )
  85. if s, ok = client.Toml2(); !ok {
  86. return errors.New("load config center error")
  87. }
  88. if _, err = toml.Decode(s, &tmpConf); err != nil {
  89. return errors.New("could not decode config")
  90. }
  91. *Conf = *tmpConf
  92. return
  93. }