package middleware import ( "net/http" "git.trcreatives.at/trcreatives/go-kite" ) type MaxBodySizeConfig struct { MaxBytes int64 } func MaxBodySize(maxBytes int64) kite.Middleware { return MaxBodySizeWithConfig(MaxBodySizeConfig{MaxBytes: maxBytes}) } func MaxBodySizeWithConfig(cfg MaxBodySizeConfig) kite.Middleware { if cfg.MaxBytes == 0 { cfg.MaxBytes = 4 << 20 } return func(h kite.Handler) kite.Handler { return func(ctx *kite.Context) error { ctx.GetRequest().Body = http.MaxBytesReader(ctx.GetResponse(), ctx.GetRequest().Body, cfg.MaxBytes) return h(ctx) } } }