conf.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/cache/redis"
  7. "go-common/library/conf"
  8. "go-common/library/database/orm"
  9. "go-common/library/database/sql"
  10. ecode "go-common/library/ecode/tip"
  11. "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/net/http/blademaster/middleware/permit"
  14. "go-common/library/net/http/blademaster/middleware/verify"
  15. "go-common/library/net/rpc"
  16. "go-common/library/net/rpc/warden"
  17. "go-common/library/net/trace"
  18. "go-common/library/queue/databus"
  19. xtime "go-common/library/time"
  20. "github.com/BurntSushi/toml"
  21. )
  22. var (
  23. confPath string
  24. client *conf.Client
  25. // Conf config
  26. Conf = &Config{}
  27. )
  28. // Config .
  29. type Config struct {
  30. Log *log.Config
  31. BM *bm.ServerConfig
  32. Verify *verify.Config
  33. Auth *permit.Config
  34. Tracer *trace.Config
  35. Redis *Redis
  36. Memcache *memcache.Config
  37. Ecode *ecode.Config
  38. RPC *RPC
  39. Host *Host
  40. // http client test
  41. HTTPClient HTTPClient
  42. // ORM
  43. ORM *orm.Config
  44. MySQL *sql.Config
  45. AegisPub *databus.Config
  46. Bfs *Bfs
  47. Bucket string
  48. Debug string
  49. Admin string // 所有业务的管理员
  50. Consumer *Consumer
  51. Gray *Gray
  52. Auditstate map[string]string
  53. GRPC *GRPC
  54. }
  55. //GRPC .
  56. type GRPC struct {
  57. AccRPC *warden.ClientConfig
  58. UpRPC *warden.ClientConfig
  59. }
  60. // Gray .
  61. type Gray struct {
  62. Biz []graybiz
  63. }
  64. type graybiz struct {
  65. BusinessID int64
  66. Options []grayoption
  67. }
  68. type grayoption struct {
  69. Fields []struct {
  70. Name string
  71. Value string
  72. }
  73. }
  74. // Consumer 在线过期时间,角色过期时间
  75. type Consumer struct {
  76. OnExp int32
  77. RoleExp int32
  78. }
  79. // Bfs reprensents the bfs config
  80. type Bfs struct {
  81. Key string
  82. Secret string
  83. Host string
  84. Timeout int
  85. MaxFileSize int
  86. }
  87. // Host host config .
  88. type Host struct {
  89. API string
  90. Manager string
  91. MainSearch string
  92. }
  93. // Redis .
  94. type Redis struct {
  95. NetExpire xtime.Duration
  96. Cluster *redis.Config
  97. }
  98. // HTTPClient str
  99. type HTTPClient struct {
  100. Read *bm.ClientConfig
  101. Write *bm.ClientConfig
  102. Es *bm.ClientConfig
  103. }
  104. //DB .
  105. type DB struct {
  106. Aegis *sql.Config
  107. MySQL *sql.Config
  108. }
  109. // RPC .
  110. type RPC struct {
  111. Acc *rpc.ClientConfig
  112. Rel *rpc.ClientConfig
  113. Up *rpc.ClientConfig
  114. }
  115. func init() {
  116. flag.StringVar(&confPath, "conf", "", "default config path")
  117. }
  118. // Init init conf
  119. func Init() error {
  120. if confPath != "" {
  121. return local()
  122. }
  123. return remote()
  124. }
  125. func local() (err error) {
  126. _, err = toml.DecodeFile(confPath, &Conf)
  127. return
  128. }
  129. func remote() (err error) {
  130. if client, err = conf.New(); err != nil {
  131. return
  132. }
  133. if err = load(); err != nil {
  134. return
  135. }
  136. go func() {
  137. for range client.Event() {
  138. log.Info("config reload")
  139. if load() != nil {
  140. log.Error("config reload error (%v)", err)
  141. }
  142. }
  143. }()
  144. return
  145. }
  146. func load() (err error) {
  147. var (
  148. s string
  149. ok bool
  150. tmpConf *Config
  151. )
  152. if s, ok = client.Toml2(); !ok {
  153. return errors.New("load config center error")
  154. }
  155. if _, err = toml.Decode(s, &tmpConf); err != nil {
  156. return errors.New("could not decode config")
  157. }
  158. *Conf = *tmpConf
  159. return
  160. }