example_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package antispam_test
  2. import (
  3. "time"
  4. "go-common/library/cache/redis"
  5. "go-common/library/container/pool"
  6. "go-common/library/net/http/blademaster"
  7. "go-common/library/net/http/blademaster/middleware/antispam"
  8. xtime "go-common/library/time"
  9. )
  10. // This example create a antispam middleware instance and attach to a blademaster engine,
  11. // it will protect '/ping' API with specified policy.
  12. // If anyone who requests this API more frequently than 1 req/second or 1 req/hour,
  13. // a StatusServiceUnavailable error will be raised.
  14. func Example() {
  15. anti := antispam.New(&antispam.Config{
  16. On: true,
  17. Second: 1,
  18. N: 1,
  19. Hour: 1,
  20. M: 1,
  21. Redis: &redis.Config{
  22. Config: &pool.Config{
  23. Active: 10,
  24. Idle: 10,
  25. IdleTimeout: xtime.Duration(time.Second * 60),
  26. },
  27. Name: "test",
  28. Proto: "tcp",
  29. Addr: "172.18.33.60:6889",
  30. DialTimeout: xtime.Duration(time.Second),
  31. ReadTimeout: xtime.Duration(time.Second),
  32. WriteTimeout: xtime.Duration(time.Second),
  33. },
  34. })
  35. engine := blademaster.Default()
  36. engine.Use(anti)
  37. engine.GET("/ping", func(c *blademaster.Context) {
  38. c.String(200, "%s", "pong")
  39. })
  40. engine.Run(":18080")
  41. }