conf.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/sql"
  8. xlog "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/middleware/auth"
  11. "go-common/library/net/http/blademaster/middleware/verify"
  12. "go-common/library/net/rpc"
  13. "go-common/library/net/trace"
  14. "go-common/library/time"
  15. "github.com/BurntSushi/toml"
  16. )
  17. var (
  18. confPath string
  19. client *conf.Client
  20. // Conf is global config
  21. Conf = &Config{}
  22. )
  23. // Config service config
  24. type Config struct {
  25. Version string `toml:"version"`
  26. Static string
  27. LocsDegrade bool
  28. // reload
  29. Reload ReloadInterval
  30. // app
  31. App *bm.App
  32. // Auth
  33. Auth *auth.Config
  34. // verify
  35. Verify *verify.Config
  36. // http
  37. BM *HTTPServers
  38. Tracer *trace.Config
  39. // db
  40. MySQL *MySQL
  41. // rpc
  42. RPCClient2 *RPCClient2
  43. // rpc Location
  44. LocationRPC *rpc.ClientConfig
  45. // httpClient
  46. HTTPClient *bm.ClientConfig
  47. // Host
  48. Host *Host
  49. // XLog
  50. XLog *xlog.Config
  51. // DegradeConfig
  52. DegradeConfig *DegradeConfig
  53. }
  54. // Host defeine host info
  55. type Host struct {
  56. Bangumi string
  57. Ad string
  58. }
  59. // HTTPServers Http Servers
  60. type HTTPServers struct {
  61. Outer *bm.ServerConfig
  62. Inner *bm.ServerConfig
  63. Local *bm.ServerConfig
  64. }
  65. // MySQL define MySQL config
  66. type MySQL struct {
  67. Operation *sql.Config
  68. Ads *sql.Config
  69. Res *sql.Config
  70. Cpt *sql.Config
  71. }
  72. // ReloadInterval define reolad config
  73. type ReloadInterval struct {
  74. Jobs time.Duration
  75. Notice time.Duration
  76. Ad time.Duration
  77. }
  78. // RPCClient2 define RPC client config
  79. type RPCClient2 struct {
  80. Archive *rpc.ClientConfig
  81. Account *rpc.ClientConfig
  82. Resource *rpc.ClientConfig
  83. }
  84. // DegradeConfig .
  85. type DegradeConfig struct {
  86. Expire int32
  87. Memcache *memcache.Config
  88. }
  89. func init() {
  90. flag.StringVar(&confPath, "conf", "", "config path")
  91. }
  92. func local() (err error) {
  93. _, err = toml.DecodeFile(confPath, &Conf)
  94. return
  95. }
  96. // Init int config
  97. func Init() error {
  98. if confPath != "" {
  99. return local()
  100. }
  101. return remote()
  102. }
  103. func remote() (err error) {
  104. if client, err = conf.New(); err != nil {
  105. return
  106. }
  107. if err = load(); err != nil {
  108. return
  109. }
  110. go func() {
  111. for range client.Event() {
  112. xlog.Info("config reload")
  113. if load() != nil {
  114. xlog.Error("config reload error (%v)", err)
  115. }
  116. }
  117. }()
  118. return
  119. }
  120. func load() (err error) {
  121. var (
  122. s string
  123. ok bool
  124. tmpConf *Config
  125. )
  126. if s, ok = client.Toml2(); !ok {
  127. return errors.New("load config center error")
  128. }
  129. if _, err = toml.Decode(s, &tmpConf); err != nil {
  130. return errors.New("could not decode config")
  131. }
  132. *Conf = *tmpConf
  133. return
  134. }