conf.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/sql"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. v "go-common/library/net/http/blademaster/middleware/verify"
  12. "go-common/library/net/rpc"
  13. "go-common/library/net/rpc/warden"
  14. "go-common/library/net/trace"
  15. "go-common/library/queue/databus"
  16. "go-common/library/time"
  17. "github.com/BurntSushi/toml"
  18. )
  19. // Conf global variable.
  20. var (
  21. Conf = &Config{}
  22. client *conf.Client
  23. confPath string
  24. )
  25. // Config struct of conf.
  26. type Config struct {
  27. Log *log.Config
  28. BM *bm.ServerConfig
  29. HTTPClient *bm.ClientConfig
  30. RPCServer *rpc.ServerConfig
  31. WardenServer *warden.ServerConfig
  32. AccMysql *sql.Config
  33. Mysql *sql.Config
  34. Memcache *memcache.Config
  35. CacheTTL *CacheTTL
  36. Tracer *trace.Config
  37. Databus *databus.Config
  38. Redis *redis.Config
  39. AccountNotify *databus.Config
  40. ReportUser *databus.Config
  41. ReportManager *databus.Config
  42. Host *Host
  43. Verify *v.Config
  44. // realname
  45. RealnameProperty *RealnameProperty
  46. // block
  47. BlockMySQL *sql.Config
  48. BlockMemcache *memcache.Config
  49. BlockProperty *BlockProperty
  50. BlockCacheTTL *BlockCacheTTL
  51. }
  52. // Host is
  53. type Host struct {
  54. Search string
  55. }
  56. // CacheTTL cache live time.
  57. type CacheTTL struct {
  58. BaseTTL time.Duration
  59. MoralTTL time.Duration
  60. CaptureTimesTTL time.Duration
  61. CaptureCodeTTL time.Duration
  62. CaptureErrTimesTTL time.Duration
  63. ApplyInfoTTL time.Duration
  64. }
  65. // RealnameProperty .
  66. type RealnameProperty struct {
  67. IMGURLTemplate string
  68. }
  69. // BlockProperty .
  70. type BlockProperty struct {
  71. MSGURL string
  72. WhiteList []int64
  73. }
  74. // BlockCacheTTL is
  75. type BlockCacheTTL struct {
  76. UserTTL time.Duration
  77. UserMaxRate float64
  78. UserT float64
  79. }
  80. func local() (err error) {
  81. _, err = toml.DecodeFile(confPath, &Conf)
  82. return
  83. }
  84. func remote() (err error) {
  85. if client, err = conf.New(); err != nil {
  86. return
  87. }
  88. if err = load(); err != nil {
  89. return
  90. }
  91. // go func() {
  92. // for range client.Event() {
  93. // log.Info("config reload")
  94. // if load() != nil {
  95. // log.Error("config reload error (%v)", err)
  96. // }
  97. // }
  98. // }()
  99. return
  100. }
  101. func load() (err error) {
  102. var (
  103. s string
  104. ok bool
  105. tmpConf *Config
  106. )
  107. if s, ok = client.Toml2(); !ok {
  108. return errors.New("load config center error")
  109. }
  110. if _, err = toml.Decode(s, &tmpConf); err != nil {
  111. return errors.New("could not decode config")
  112. }
  113. *Conf = *tmpConf
  114. return
  115. }
  116. func init() {
  117. flag.StringVar(&confPath, "conf", "", "default config path")
  118. }
  119. // Init int config
  120. func Init() error {
  121. if confPath != "" {
  122. return local()
  123. }
  124. return remote()
  125. }
  126. // RsaPub is realname rsa pub key
  127. func RsaPub() (key string) {
  128. if client == nil {
  129. return ""
  130. }
  131. key, _ = client.Value("realname.rsa.pub")
  132. return
  133. }
  134. // RsaPriv is realname rsa priv key
  135. func RsaPriv() (key string) {
  136. if client == nil {
  137. return ""
  138. }
  139. key, _ = client.Value("realname.rsa.priv")
  140. return
  141. }