conf.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/sql"
  8. ecode "go-common/library/ecode/tip"
  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"
  14. "go-common/library/net/trace"
  15. "go-common/library/time"
  16. "github.com/BurntSushi/toml"
  17. )
  18. const (
  19. configKey = "antispam-service.toml"
  20. )
  21. var (
  22. // Conf .
  23. Conf *Config
  24. // ConfPath .
  25. ConfPath string
  26. )
  27. // Config .
  28. type Config struct {
  29. RPC *rpc.ServerConfig
  30. App *bm.App
  31. BM *bm.ServerConfig
  32. HTTPClient *bm.ClientConfig
  33. MySQL *MySQL
  34. Redis *Redis
  35. Tracer *trace.Config
  36. Log *log.Config
  37. Verify *verify.Config
  38. Auth *auth.Config
  39. Ecode *ecode.Config
  40. AppkeyType map[string][]int8
  41. ReplyURL string
  42. ServiceOption *ServiceOption
  43. MaxSpawnGoroutines int
  44. MaxAllowedCounts int64
  45. MaxDurationSec int64
  46. AutoWhite *AutoWhite
  47. }
  48. // AutoWhite .
  49. type AutoWhite struct {
  50. KeywordHitCounts int64
  51. NumOfSenders int64
  52. Derivation float64
  53. }
  54. // ServiceOption .
  55. type ServiceOption struct {
  56. GcOpt *GcOpt
  57. BuildTrieIntervalMinute int64
  58. BuildTrieMaxRowsPerQuery int64
  59. AsyncTaskChanSize int64
  60. RefreshTrieIntervalSec int64
  61. RefreshRulesIntervalSec int64
  62. RefreshRegexpsIntervalSec int64
  63. MinKeywordLen int
  64. MaxSenderNum int64
  65. DefaultExpireSec int64
  66. DefaultChanSize int64
  67. MaxExportRows int64
  68. MaxRegexpCountsPerArea int64
  69. MaxSpawnGoroutines int64
  70. RuleDefaultExpireSec int64
  71. RegexpDefaultExpireSec int64
  72. }
  73. // GcOpt .
  74. type GcOpt struct {
  75. Open bool
  76. IntervalSec int
  77. MaxRowsPerQuery int64
  78. }
  79. // MySQL .
  80. type MySQL struct {
  81. AntiSpam *sql.Config
  82. }
  83. // Redis .
  84. type Redis struct {
  85. *redis.Config
  86. IndexExpire time.Duration
  87. }
  88. func init() {
  89. flag.StringVar(&ConfPath, "conf", "", "config path")
  90. }
  91. // Init .
  92. func Init(path string) error {
  93. if len(Areas) == 0 {
  94. panic(errors.New("areas must be set"))
  95. }
  96. if path == "" {
  97. return configCenter()
  98. }
  99. _, err := toml.DecodeFile(path, &Conf)
  100. return err
  101. }
  102. func configCenter() error {
  103. client, err := conf.New()
  104. if err != nil {
  105. return err
  106. }
  107. value, ok := client.Value(configKey)
  108. if !ok {
  109. return errors.New("empty value")
  110. }
  111. _, err = toml.Decode(value, &Conf)
  112. return err
  113. }
  114. // Areas .
  115. var Areas = map[string]int{
  116. "reply": 1,
  117. "im": 2,
  118. "live_dm": 3,
  119. "danmu": 4,
  120. }