conf.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/database/orm"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/http/blademaster/middleware/permit"
  10. "go-common/library/net/rpc"
  11. "github.com/BurntSushi/toml"
  12. )
  13. var (
  14. confPath string
  15. // Conf conf
  16. Conf = &Config{}
  17. client *conf.Client
  18. )
  19. // Config config
  20. type Config struct {
  21. // identify
  22. Auth *permit.Config
  23. // log
  24. Log *log.Config
  25. // orm
  26. ORM *orm.Config
  27. // http client
  28. HTTPClient *bm.ClientConfig
  29. //ConfSvr
  30. ConfSvr *rpc.ClientConfig
  31. //tree
  32. Tree *ServiceTree
  33. // db apm
  34. ORMApm *orm.Config
  35. //BM
  36. BM *bm.ServerConfig
  37. }
  38. // ServiceTree service tree.
  39. type ServiceTree struct {
  40. Platform string
  41. }
  42. func init() {
  43. flag.StringVar(&confPath, "conf", "", "config file")
  44. }
  45. // Init init.
  46. func Init() (err error) {
  47. if confPath != "" {
  48. return local()
  49. }
  50. return remote()
  51. }
  52. func local() (err error) {
  53. _, err = toml.DecodeFile(confPath, &Conf)
  54. return
  55. }
  56. func remote() (err error) {
  57. if client, err = conf.New(); err != nil {
  58. return
  59. }
  60. if err = load(); err != nil {
  61. return
  62. }
  63. go func() {
  64. for range client.Event() {
  65. log.Info("config reload")
  66. if err := load(); err != nil {
  67. log.Error("config reload error (%v)", err)
  68. }
  69. }
  70. }()
  71. return
  72. }
  73. func load() (err error) {
  74. var (
  75. s string
  76. ok bool
  77. tmpConf *Config
  78. )
  79. if s, ok = client.Toml2(); !ok {
  80. return errors.New("load config center error")
  81. }
  82. if _, err = toml.Decode(s, &tmpConf); err != nil {
  83. return errors.New("could not decode config")
  84. }
  85. *Conf = *tmpConf
  86. return
  87. }