conf.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/app/admin/main/block/model"
  6. "go-common/library/cache/memcache"
  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/permit"
  13. "go-common/library/net/rpc"
  14. "go-common/library/net/trace"
  15. "go-common/library/queue/databus"
  16. "github.com/BurntSushi/toml"
  17. )
  18. // global var
  19. var (
  20. confPath string
  21. client *conf.Client
  22. // Conf config
  23. Conf = &Config{}
  24. )
  25. // Config config set
  26. type Config struct {
  27. Log *log.Config
  28. Tracer *trace.Config
  29. Auth *permit.Config
  30. Ecode *ecode.Config
  31. BM *bm.ServerConfig
  32. RPCClients *RPC
  33. Memcache *memcache.Config
  34. MySQL *sql.Config
  35. HTTPClient *bm.ClientConfig
  36. Perms *Perms
  37. AccountNotify *databus.Config
  38. Property *Property
  39. // manager log config
  40. ManagerLog *databus.Config
  41. }
  42. // Property .
  43. type Property struct {
  44. BlackHouseURL string
  45. MSGURL string
  46. TelURL string
  47. MailURL string
  48. MSG *MSG
  49. }
  50. // MSG .
  51. type MSG struct {
  52. BlackHouseLimit model.MSG
  53. BlackHouseForever model.MSG
  54. SysLimit model.MSG
  55. SysForever model.MSG
  56. BlockRemove model.MSG
  57. }
  58. // RPC .
  59. type RPC struct {
  60. Account *rpc.ClientConfig
  61. Figure *rpc.ClientConfig
  62. Spy *rpc.ClientConfig
  63. }
  64. // Perms .
  65. type Perms struct {
  66. Perm map[string]string
  67. }
  68. func init() {
  69. flag.StringVar(&confPath, "conf", "", "default config path")
  70. }
  71. // Init init conf
  72. func Init() error {
  73. if confPath != "" {
  74. return local()
  75. }
  76. return remote()
  77. }
  78. func local() (err error) {
  79. _, err = toml.DecodeFile(confPath, &Conf)
  80. return
  81. }
  82. func remote() (err error) {
  83. if client, err = conf.New(); err != nil {
  84. return
  85. }
  86. if err = load(); err != nil {
  87. return
  88. }
  89. go func() {
  90. for range client.Event() {
  91. log.Info("config reload")
  92. if load() != nil {
  93. log.Error("config reload error (%v)", err)
  94. }
  95. }
  96. }()
  97. return
  98. }
  99. func load() (err error) {
  100. var (
  101. s string
  102. ok bool
  103. tmpConf *Config
  104. )
  105. if s, ok = client.Toml2(); !ok {
  106. return errors.New("load config center error")
  107. }
  108. if _, err = toml.Decode(s, &tmpConf); err != nil {
  109. return errors.New("could not decode config")
  110. }
  111. *Conf = *tmpConf
  112. return
  113. }