conf.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/conf"
  7. "go-common/library/database/orm"
  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/rpc"
  12. "go-common/library/net/rpc/warden"
  13. "go-common/library/net/trace"
  14. "go-common/library/queue/databus"
  15. xtime "go-common/library/time"
  16. "github.com/BurntSushi/toml"
  17. )
  18. var (
  19. confPath string
  20. client *conf.Client
  21. // Conf of config
  22. Conf = &Config{}
  23. )
  24. // Memcache memcache.
  25. type Memcache struct {
  26. *memcache.Config
  27. }
  28. // Bfs Bfs.
  29. type Bfs struct {
  30. Timeout xtime.Duration
  31. MaxFileSize int
  32. Bucket string
  33. Addr string
  34. Key string
  35. Secret string
  36. }
  37. // Cfg def
  38. type Cfg struct {
  39. // HotCroFre hotword crontab frequency
  40. HotCroFre string
  41. // DarkCroFre darkword crontab frequency
  42. DarkCroFre string
  43. //RunCront is run crontab
  44. RunCront bool
  45. }
  46. //Host host
  47. type Host struct {
  48. Manager string
  49. }
  50. // Config def.
  51. type Config struct {
  52. // base
  53. // http
  54. HTTPServer *bm.ServerConfig
  55. // httpClinet
  56. HTTPClient *bm.ClientConfig
  57. // host
  58. Host *Host
  59. // auth
  60. Auth *permit.Config
  61. // db
  62. ORM *orm.Config
  63. // db
  64. ORMResource *orm.Config
  65. // log
  66. Log *log.Config
  67. // tracer
  68. Tracer *trace.Config
  69. //mc
  70. Memcache *Memcache
  71. // Bfs
  72. Bfs *Bfs
  73. // log
  74. ManagerReport *databus.Config
  75. // BroadcastRPC grpc
  76. PGCRPC *warden.ClientConfig
  77. // rpc client
  78. AccountRPC *rpc.ClientConfig
  79. ArchiveRPC *rpc.ClientConfig
  80. // Cfg
  81. Cfg *Cfg
  82. }
  83. func local() (err error) {
  84. _, err = toml.DecodeFile(confPath, &Conf)
  85. return
  86. }
  87. func remote() (err error) {
  88. if client, err = conf.New(); err != nil {
  89. return
  90. }
  91. if err = load(); err != nil {
  92. return
  93. }
  94. go func() {
  95. for range client.Event() {
  96. log.Info("config reload")
  97. if load() != nil {
  98. log.Error("config reload error (%v)", err)
  99. }
  100. }
  101. }()
  102. return
  103. }
  104. func load() (err error) {
  105. var (
  106. s string
  107. ok bool
  108. tmpConf *Config
  109. )
  110. if s, ok = client.Toml2(); !ok {
  111. return errors.New("load config center error")
  112. }
  113. if _, err = toml.Decode(s, &tmpConf); err != nil {
  114. return errors.New("could not decode config")
  115. }
  116. *Conf = *tmpConf
  117. return
  118. }
  119. func init() {
  120. flag.StringVar(&confPath, "conf", "", "default config path")
  121. }
  122. // Init int config
  123. func Init() error {
  124. if confPath != "" {
  125. return local()
  126. }
  127. return remote()
  128. }