conf.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. "go-common/library/net/trace"
  12. "go-common/library/queue/databus"
  13. "github.com/BurntSushi/toml"
  14. )
  15. var (
  16. confPath string
  17. client *conf.Client
  18. // Conf of config
  19. Conf = &Config{}
  20. )
  21. // Config def.
  22. type Config struct {
  23. // http
  24. BM *bm.ServerConfig
  25. // auth
  26. Permit *permit.Config
  27. // db
  28. ORM *orm.Config
  29. // log
  30. Log *log.Config
  31. // tracer
  32. Tracer *trace.Config
  33. // RelationRPC
  34. RelationRPC *rpc.ClientConfig
  35. // manager report
  36. ManagerReport *databus.Config
  37. }
  38. func local() (err error) {
  39. _, err = toml.DecodeFile(confPath, &Conf)
  40. return
  41. }
  42. func remote() (err error) {
  43. if client, err = conf.New(); err != nil {
  44. return
  45. }
  46. if err = load(); err != nil {
  47. return
  48. }
  49. go func() {
  50. for range client.Event() {
  51. log.Info("config reload")
  52. if load() != nil {
  53. log.Error("config reload error (%v)", err)
  54. }
  55. }
  56. }()
  57. return
  58. }
  59. func load() (err error) {
  60. var (
  61. s string
  62. ok bool
  63. tmpConf *Config
  64. )
  65. if s, ok = client.Value("space-admin.toml"); !ok {
  66. return errors.New("load config center error")
  67. }
  68. if _, err = toml.Decode(s, &tmpConf); err != nil {
  69. return errors.New("could not decode config")
  70. }
  71. *Conf = *tmpConf
  72. return
  73. }
  74. func init() {
  75. flag.StringVar(&confPath, "conf", "", "default config path")
  76. }
  77. // Init int config
  78. func Init() error {
  79. if confPath != "" {
  80. return local()
  81. }
  82. return remote()
  83. }