response size limiter

响应长度限制中间件,可以限制响应数据的长度,避免返回过大的数据导致网络占用过大。此中间件主要用于避免一些非法调用等导致查询过多数据。

Example

  1. package main
  2. import (
  3. "bytes"
  4. "time"
  5. "github.com/vicanso/elton"
  6. "github.com/vicanso/elton/middleware"
  7. )
  8. func main() {
  9. e := elton.New()
  10. e.Use(middleware.NewResponseSizeLimiter(middleware.ResponseSizeLimiterConfig{
  11. // 1MB
  12. MaxSize: 1024 * 1024,
  13. }))
  14. e.GET("/users/me", func(c *elton.Context) (err error) {
  15. time.Sleep(time.Second)
  16. c.BodyBuffer = bytes.NewBufferString(`{
  17. "account": "tree",
  18. "name": "tree.xie"
  19. }`)
  20. return nil
  21. })
  22. err := e.ListenAndServe(":3000")
  23. if err != nil {
  24. panic(err)
  25. }
  26. }