conf.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/rpc"
  10. "go-common/library/net/trace"
  11. "go-common/library/time"
  12. "github.com/BurntSushi/toml"
  13. )
  14. var (
  15. confPath string
  16. Conf = &Config{}
  17. client *conf.Client
  18. )
  19. type Config struct {
  20. // base
  21. SeqSvrs []string
  22. Tick time.Duration
  23. // rpc server
  24. RPCServer *rpc.ServerConfig
  25. // http
  26. BM *bm.ServerConfig
  27. // log
  28. Log *log.Config
  29. // tracer
  30. Tracer *trace.Config
  31. // db
  32. DB *DB
  33. }
  34. type DB struct {
  35. Number *sql.Config
  36. }
  37. func init() {
  38. flag.StringVar(&confPath, "conf", "", "default config path")
  39. }
  40. // Init init config.
  41. func Init() (err error) {
  42. if confPath != "" {
  43. _, err = toml.DecodeFile(confPath, &Conf)
  44. return
  45. }
  46. err = remote()
  47. return
  48. }
  49. func remote() (err error) {
  50. if client, err = conf.New(); err != nil {
  51. return
  52. }
  53. if err = load(); err != nil {
  54. return
  55. }
  56. go func() {
  57. for range client.Event() {
  58. log.Info("config reload")
  59. if load() != nil {
  60. log.Error("config reload error (%v)", err)
  61. }
  62. }
  63. }()
  64. return
  65. }
  66. func load() (err error) {
  67. var (
  68. s string
  69. ok bool
  70. tmpConf = &Config{}
  71. )
  72. if s, ok = client.Toml2(); !ok {
  73. return errors.New("load config center error")
  74. }
  75. if _, err = toml.Decode(s, tmpConf); err != nil {
  76. return errors.New("could not decode config")
  77. }
  78. *Conf = *tmpConf
  79. return
  80. }