response size limiter
响应长度限制中间件,可以限制响应数据的长度,避免返回过大的数据导致网络占用过大。此中间件主要用于避免一些非法调用等导致查询过多数据。
Example
package main
import (
"bytes"
"time"
"github.com/vicanso/elton"
"github.com/vicanso/elton/middleware"
)
func main() {
e := elton.New()
e.Use(middleware.NewResponseSizeLimiter(middleware.ResponseSizeLimiterConfig{
// 1MB
MaxSize: 1024 * 1024,
}))
e.GET("/users/me", func(c *elton.Context) (err error) {
time.Sleep(time.Second)
c.BodyBuffer = bytes.NewBufferString(`{
"account": "tree",
"name": "tree.xie"
}`)
return nil
})
err := e.ListenAndServe(":3000")
if err != nil {
panic(err)
}
}