conf.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/database/sql"
  7. ecode "go-common/library/ecode/tip"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/middleware/permit"
  11. "go-common/library/net/trace"
  12. "github.com/BurntSushi/toml"
  13. )
  14. var (
  15. confPath string
  16. // Conf .
  17. Conf = &Config{}
  18. client *conf.Client
  19. )
  20. // Pagination .
  21. type Pagination struct {
  22. PageNum int
  23. PageSize int
  24. MaxPageNum int
  25. MaxPageSize int
  26. }
  27. // Config .
  28. type Config struct {
  29. Auth *permit.Config
  30. // base
  31. // xlog
  32. Log *log.Config
  33. // tracer
  34. Tracer *trace.Config
  35. // http
  36. BM *bm.ServerConfig
  37. // es cluster
  38. Es map[string]*EsInfo
  39. // ecode
  40. Ecode *ecode.Config
  41. Pagination *Pagination
  42. // sms
  43. SMS *SMS
  44. // db
  45. DB *DB
  46. // httpclient
  47. HTTPClient *bm.ClientConfig
  48. Prop *Properties
  49. }
  50. // Properties .
  51. type Properties struct {
  52. Manager string
  53. API string
  54. }
  55. // EsInfo .
  56. type EsInfo struct {
  57. Addr []string
  58. Cluster string
  59. Owner string
  60. }
  61. // SMS config
  62. type SMS struct {
  63. Phone string
  64. Token string
  65. Interval int64
  66. }
  67. func init() {
  68. flag.StringVar(&confPath, "conf", "", "default config path")
  69. }
  70. // Init init config.
  71. func Init() (err error) {
  72. if confPath != "" {
  73. _, err = toml.DecodeFile(confPath, &Conf)
  74. return
  75. }
  76. err = remote()
  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. }
  111. // DB is the workflow db config model
  112. type DB struct {
  113. Search *sql.Config
  114. }