conf.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/netutil"
  10. "go-common/library/queue/databus"
  11. xtime "go-common/library/time"
  12. "github.com/BurntSushi/toml"
  13. )
  14. var (
  15. confPath string
  16. client *conf.Client
  17. // Conf service config
  18. Conf = &Config{}
  19. )
  20. // Config def.
  21. type Config struct {
  22. Log *log.Config
  23. Databus *DataSource
  24. Mysql *sql.Config
  25. BFS *BFS
  26. HTTPClient *bm.ClientConfig
  27. Properties *Properties
  28. Backoff *netutil.BackoffConfig
  29. BM *bm.ServerConfig
  30. }
  31. // BFS bfs config
  32. type BFS struct {
  33. Timeout xtime.Duration
  34. MaxFileSize int
  35. Bucket string
  36. URL string
  37. Method string
  38. Key string
  39. Secret string
  40. Host string
  41. }
  42. // Properties def.
  43. type Properties struct {
  44. UploadInterval xtime.Duration
  45. AccountIntranetURI string
  46. MaxRetries int
  47. FontFilePath string
  48. }
  49. // DataSource databus source
  50. type DataSource struct {
  51. Labour *databus.Config
  52. Account *databus.Config
  53. }
  54. func init() {
  55. flag.StringVar(&confPath, "conf", "", "config path")
  56. }
  57. // Init init conf.
  58. func Init() (err error) {
  59. if confPath == "" {
  60. return configCenter()
  61. }
  62. _, err = toml.DecodeFile(confPath, &Conf)
  63. return
  64. }
  65. func configCenter() (err error) {
  66. if client, err = conf.New(); err != nil {
  67. panic(err)
  68. }
  69. if err = load(); err != nil {
  70. return
  71. }
  72. go func() {
  73. for range client.Event() {
  74. log.Info("config reload")
  75. if load() != nil {
  76. log.Error("config reload error (%v)", err)
  77. }
  78. }
  79. }()
  80. return
  81. }
  82. func load() (err error) {
  83. var (
  84. s string
  85. ok bool
  86. tmpConf *Config
  87. )
  88. if s, ok = client.Toml2(); !ok {
  89. return errors.New("load config center error")
  90. }
  91. if _, err = toml.Decode(s, &tmpConf); err != nil {
  92. return errors.New("could not decode config")
  93. }
  94. *Conf = *tmpConf
  95. return
  96. }