conf.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/redis"
  6. "go-common/library/conf"
  7. "go-common/library/database/orm"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/http/blademaster/middleware/auth"
  12. "go-common/library/net/http/blademaster/middleware/verify"
  13. "go-common/library/net/rpc/warden"
  14. "go-common/library/time"
  15. "github.com/BurntSushi/toml"
  16. )
  17. var (
  18. // Conf common conf
  19. Conf = &Config{}
  20. client *conf.Client
  21. confPath string
  22. )
  23. // Config config struct
  24. type Config struct {
  25. // base
  26. // 数据库配置
  27. DB *DB
  28. // redis
  29. Redis *Redis
  30. // http client
  31. HTTPClient HTTPClient
  32. // http
  33. BM *HTTPServers
  34. // grpc server
  35. RPCServer *warden.ServerConfig
  36. // log
  37. Log *log.Config
  38. // auth
  39. Auth *auth.Config
  40. Verify *verify.Config
  41. // orm
  42. ORM *orm.Config
  43. // UT
  44. UT *UT
  45. // URL
  46. URL *URL
  47. // BASECenter
  48. BASECenter *BASECenter
  49. // Tag
  50. Tag *Tag
  51. }
  52. // HTTPClient config
  53. type HTTPClient struct {
  54. Read *bm.ClientConfig
  55. Write *bm.ClientConfig
  56. }
  57. // URL external resources
  58. type URL struct {
  59. ElasticHost string
  60. DefaultHead string
  61. }
  62. // BASECenter config
  63. type BASECenter struct {
  64. AppID string
  65. AppToken string
  66. URL string
  67. }
  68. // HTTPServers Http Servers
  69. type HTTPServers struct {
  70. Inner *bm.ServerConfig
  71. Local *bm.ServerConfig
  72. }
  73. // Redis config
  74. type Redis struct {
  75. Master *redis.Config
  76. Expire time.Duration
  77. }
  78. // DB config
  79. type DB struct {
  80. Master *sql.Config
  81. }
  82. // UT config
  83. type UT struct {
  84. DistPrefix string
  85. }
  86. // Tag config
  87. type Tag struct {
  88. Tags string
  89. }
  90. func local() (err error) {
  91. _, err = toml.DecodeFile(confPath, &Conf)
  92. return
  93. }
  94. func remote() (err error) {
  95. if client, err = conf.New(); err != nil {
  96. return
  97. }
  98. if err = load(); err != nil {
  99. return
  100. }
  101. go func() {
  102. for range client.Event() {
  103. log.Info("config event")
  104. }
  105. }()
  106. return
  107. }
  108. func load() (err error) {
  109. var (
  110. s string
  111. ok bool
  112. tmpConf *Config
  113. )
  114. if s, ok = client.Value("item.toml"); !ok {
  115. return errors.New("load config center error")
  116. }
  117. if _, err = toml.Decode(s, &tmpConf); err != nil {
  118. return errors.New("could not decode config")
  119. }
  120. *Conf = *tmpConf
  121. return
  122. }
  123. func init() {
  124. flag.StringVar(&confPath, "conf", "", "default config path")
  125. }
  126. // Init init config
  127. func Init() error {
  128. if confPath == "" {
  129. return remote()
  130. }
  131. return local()
  132. }