diff --git a/middleware/compress.go b/middleware/compress.go index 5974371c..6329cf98 100644 --- a/middleware/compress.go +++ b/middleware/compress.go @@ -27,6 +27,13 @@ var CompressConfigDefault = CompressConfig{ Level: CompressLevelDefault, } +var compressHandlers = map[int]fasthttp.RequestHandler{ + CompressLevelDisabled: fasthttp.CompressHandlerBrotliLevel(func(c *fasthttp.RequestCtx) {}, fasthttp.CompressBrotliNoCompression, fasthttp.CompressNoCompression), + CompressLevelDefault: fasthttp.CompressHandlerBrotliLevel(func(c *fasthttp.RequestCtx) {}, fasthttp.CompressBrotliDefaultCompression, fasthttp.CompressDefaultCompression), + CompressLevelBestSpeed: fasthttp.CompressHandlerBrotliLevel(func(c *fasthttp.RequestCtx) {}, fasthttp.CompressBrotliBestSpeed, fasthttp.CompressBestSpeed), + CompressLevelBestCompression: fasthttp.CompressHandlerBrotliLevel(func(c *fasthttp.RequestCtx) {}, fasthttp.CompressBrotliBestCompression, fasthttp.CompressBestCompression), +} + /* Compress allows the following config arguments in any order: - Compress() @@ -58,17 +65,12 @@ func Compress(options ...interface{}) fiber.Handler { func compress(config CompressConfig) fiber.Handler { // Init middleware settings - var compressHandler fasthttp.RequestHandler - switch config.Level { - case -1: // Disabled - compressHandler = fasthttp.CompressHandlerBrotliLevel(func(c *fasthttp.RequestCtx) {}, fasthttp.CompressBrotliNoCompression, fasthttp.CompressNoCompression) - case 1: // Best speed - compressHandler = fasthttp.CompressHandlerBrotliLevel(func(c *fasthttp.RequestCtx) {}, fasthttp.CompressBrotliBestSpeed, fasthttp.CompressBestSpeed) - case 2: // Best compression - compressHandler = fasthttp.CompressHandlerBrotliLevel(func(c *fasthttp.RequestCtx) {}, fasthttp.CompressBrotliBestCompression, fasthttp.CompressBestCompression) - default: // Default - compressHandler = fasthttp.CompressHandlerBrotliLevel(func(c *fasthttp.RequestCtx) {}, fasthttp.CompressBrotliDefaultCompression, fasthttp.CompressDefaultCompression) + compressHandler, ok := compressHandlers[config.Level] + if !ok { + // Use default level if provided level is invalid + compressHandler = compressHandlers[CompressLevelDefault] } + // Return handler return func(c *fiber.Ctx) { // Don't execute the middleware if Next returns false diff --git a/middleware/compress_test.go b/middleware/compress_test.go index 36eeda80..83ac58f6 100644 --- a/middleware/compress_test.go +++ b/middleware/compress_test.go @@ -144,7 +144,7 @@ func Test_Middleware_Compress_Panic(t *testing.T) { Compress("invalid") } -// go test -v -run=^$ -bench=Benchmark_Middleware_Compress -benchmem -count=4 +// go test -v ./... -run=^$ -bench=Benchmark_Middleware_Compress -benchmem -count=4 func Benchmark_Middleware_Compress(b *testing.B) { app := fiber.New() app.Use(Compress())