conf.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/app/admin/main/mcn/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/warden"
  14. "go-common/library/net/trace"
  15. "go-common/library/queue/databus"
  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. Tracer *trace.Config
  29. Memcache *memcache.Config
  30. MySQL *sql.Config
  31. Ecode *ecode.Config
  32. Auth *permit.Config
  33. BFS *BFS
  34. Host *Host
  35. GRPCClient *GRPCClient
  36. // http client
  37. HTTPClient *bm.ClientConfig
  38. // manager log config
  39. ManagerLog *databus.Config
  40. Property *Property
  41. }
  42. // Property .
  43. type Property struct {
  44. MSG []*model.MSG
  45. }
  46. // GRPCClient .
  47. type GRPCClient struct {
  48. Account *warden.ClientConfig
  49. Member *warden.ClientConfig
  50. Archive *warden.ClientConfig
  51. }
  52. // Host host config .
  53. type Host struct {
  54. Bfs string
  55. Msg string
  56. Videoup string
  57. API string
  58. }
  59. // BFS bfs config
  60. type BFS struct {
  61. Bucket string
  62. Key string
  63. Secret string
  64. }
  65. func init() {
  66. flag.StringVar(&confPath, "conf", "", "default config path")
  67. }
  68. // Init init conf
  69. func Init() error {
  70. if confPath != "" {
  71. return local()
  72. }
  73. return remote()
  74. }
  75. func local() (err error) {
  76. _, err = toml.DecodeFile(confPath, &Conf)
  77. return
  78. }
  79. func remote() (err error) {
  80. if client, err = conf.New(); err != nil {
  81. return
  82. }
  83. if err = load(); err != nil {
  84. return
  85. }
  86. go func() {
  87. for range client.Event() {
  88. log.Info("config reload")
  89. if load() != nil {
  90. log.Error("config reload error (%v)", err)
  91. }
  92. }
  93. }()
  94. return
  95. }
  96. func load() (err error) {
  97. var (
  98. s string
  99. ok bool
  100. tmpConf *Config
  101. )
  102. if s, ok = client.Toml2(); !ok {
  103. return errors.New("load config center error")
  104. }
  105. if _, err = toml.Decode(s, &tmpConf); err != nil {
  106. return errors.New("could not decode config")
  107. }
  108. *Conf = *tmpConf
  109. return
  110. }