conf.go 1.7 KB

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