conf.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/database/orm"
  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. 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. Tracer *trace.Config
  25. Ecode *ecode.Config
  26. ORM *orm.Config
  27. //auth
  28. Permit *permit.Config2
  29. // bfs
  30. Bfs *Bfs
  31. }
  32. // Bfs reprensents the bfs config
  33. type Bfs struct {
  34. Key string
  35. Secret string
  36. Host string
  37. Timeout int
  38. MaxFileSize int
  39. }
  40. func init() {
  41. flag.StringVar(&confPath, "conf", "", "default config path")
  42. }
  43. // Init init conf
  44. func Init() error {
  45. if confPath != "" {
  46. return local()
  47. }
  48. return remote()
  49. }
  50. func local() (err error) {
  51. _, err = toml.DecodeFile(confPath, &Conf)
  52. return
  53. }
  54. func remote() (err error) {
  55. if client, err = conf.New(); err != nil {
  56. return
  57. }
  58. if err = load(); err != nil {
  59. return
  60. }
  61. go func() {
  62. for range client.Event() {
  63. log.Info("config reload")
  64. if load() != nil {
  65. log.Error("config reload error (%v)", err)
  66. }
  67. }
  68. }()
  69. return
  70. }
  71. func load() (err error) {
  72. var (
  73. s string
  74. ok bool
  75. tmpConf *Config
  76. )
  77. if s, ok = client.Toml2(); !ok {
  78. return errors.New("load config center error")
  79. }
  80. if _, err = toml.Decode(s, &tmpConf); err != nil {
  81. return errors.New("could not decode config")
  82. }
  83. *Conf = *tmpConf
  84. return
  85. }