conf.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/rpc/warden"
  12. "go-common/library/net/trace"
  13. "github.com/BurntSushi/toml"
  14. )
  15. // Conf global variable.
  16. var (
  17. // config
  18. confPath string
  19. client *conf.Client
  20. Conf = &Config{}
  21. )
  22. // Config struct of conf.
  23. type Config struct {
  24. // base
  25. App *bm.App
  26. BFS *BFS
  27. // http
  28. BM *bm.ServerConfig
  29. // db
  30. ORM *ORM
  31. // host
  32. Host *Host
  33. // log
  34. Xlog *log.Config
  35. // tracer
  36. Tracer *trace.Config
  37. // rpc client
  38. RPCClient *RPC
  39. AccClient *warden.ClientConfig
  40. // http client
  41. HTTPClient *bm.ClientConfig
  42. // chan
  43. ChanSize *ChanSize
  44. Auth *permit.Config
  45. CoinClient *warden.ClientConfig
  46. }
  47. // ORM orm write and read config.
  48. type ORM struct {
  49. Write *orm.Config
  50. Read *orm.Config
  51. }
  52. // Host host config .
  53. type Host struct {
  54. Bfs string
  55. Manager string
  56. Msg string
  57. }
  58. // ChanSize sysmsg channel size.
  59. type ChanSize struct {
  60. SysMsg int64
  61. }
  62. // BFS bfs config
  63. type BFS struct {
  64. Key string
  65. Secret string
  66. }
  67. // RPC rpc client config.
  68. type RPC struct {
  69. Account *rpc.ClientConfig
  70. Relation *rpc.ClientConfig
  71. }
  72. func local() (err error) {
  73. _, err = toml.DecodeFile(confPath, &Conf)
  74. return
  75. }
  76. func remote() (err error) {
  77. if client, err = conf.New(); err != nil {
  78. return
  79. }
  80. if err = load(); err != nil {
  81. return
  82. }
  83. go func() {
  84. for range client.Event() {
  85. log.Info("config reload")
  86. if load() != nil {
  87. log.Error("config reload error (%v)", err)
  88. }
  89. }
  90. }()
  91. return
  92. }
  93. func load() (err error) {
  94. var (
  95. s string
  96. ok bool
  97. tmpConf *Config
  98. )
  99. if s, ok = client.Toml2(); !ok {
  100. return errors.New("load config center error")
  101. }
  102. if _, err = toml.Decode(s, &tmpConf); err != nil {
  103. return errors.New("could not decode config")
  104. }
  105. *Conf = *tmpConf
  106. return
  107. }
  108. func init() {
  109. flag.StringVar(&confPath, "conf", "", "default config path")
  110. }
  111. // Init int config
  112. func Init() error {
  113. if confPath != "" {
  114. return local()
  115. }
  116. return remote()
  117. }