conf.go 1.6 KB

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