whitelist.go 938 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package server
  2. import (
  3. "go-common/app/interface/main/broadcast/conf"
  4. "log"
  5. "os"
  6. )
  7. var whitelist *Whitelist
  8. // Whitelist .
  9. type Whitelist struct {
  10. log *log.Logger
  11. list map[int64]struct{} // whitelist for debug
  12. }
  13. // InitWhitelist a whitelist struct.
  14. func InitWhitelist(c *conf.Whitelist) (err error) {
  15. var (
  16. mid int64
  17. f *os.File
  18. )
  19. if f, err = os.OpenFile(c.WhiteLog, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0644); err == nil {
  20. whitelist = new(Whitelist)
  21. whitelist.log = log.New(f, "", log.LstdFlags)
  22. whitelist.list = make(map[int64]struct{})
  23. for _, mid = range c.Whitelist {
  24. whitelist.list[mid] = struct{}{}
  25. }
  26. }
  27. return
  28. }
  29. // Contains whitelist contains a mid or not.
  30. func (w *Whitelist) Contains(mid int64) (ok bool) {
  31. if mid > 0 {
  32. _, ok = w.list[mid]
  33. }
  34. return
  35. }
  36. // Printf calls l.Output to print to the logger.
  37. func (w *Whitelist) Printf(format string, v ...interface{}) {
  38. w.log.Printf(format, v...)
  39. }