1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-24 21:04:28 +00:00
fiber/middleware/compress.go

94 lines
2.8 KiB
Go
Raw Permalink Normal View History

2020-06-06 07:27:26 +02:00
package middleware
import (
"fmt"
2020-07-02 11:56:07 +02:00
"log"
2020-07-04 08:42:42 +02:00
fiber "github.com/gofiber/fiber"
fasthttp "github.com/valyala/fasthttp"
2020-06-06 07:27:26 +02:00
)
2020-07-02 11:56:07 +02:00
// CompressConfig defines the config for Compress middleware.
type CompressConfig struct {
// Next defines a function to skip this middleware if returned true.
Next func(ctx *fiber.Ctx) bool
// Compression level for brotli, gzip and deflate
Level int
}
2020-06-06 07:27:26 +02:00
// Compression levels
const (
CompressLevelDisabled = -1
CompressLevelDefault = 0
CompressLevelBestSpeed = 1
CompressLevelBestCompression = 2
)
2020-06-08 02:55:19 +02:00
// CompressConfigDefault is the default config
2020-06-06 07:27:26 +02:00
var CompressConfigDefault = CompressConfig{
Next: nil,
Level: CompressLevelDefault,
}
2020-07-02 11:56:07 +02:00
/*
Compress allows the following config arguments in any order:
- Compress()
- Compress(next func(*fiber.Ctx) bool)
- Compress(level int)
- Compress(config CompressConfig)
2020-07-02 11:56:07 +02:00
*/
func Compress(options ...interface{}) fiber.Handler {
2020-06-06 07:27:26 +02:00
// Create default config
var config = CompressConfigDefault
2020-07-02 11:56:07 +02:00
// Assert options if provided to adjust the config
if len(options) > 0 {
for i := range options {
switch opt := options[i].(type) {
case func(*fiber.Ctx) bool:
config.Next = opt
case int:
config.Level = opt
case CompressConfig:
config = opt
2020-07-02 11:56:07 +02:00
default:
log.Fatal("Compress: the following option types are allowed: int, func(*fiber.Ctx) bool, CompressConfig")
2020-07-02 11:56:07 +02:00
}
}
2020-06-06 07:27:26 +02:00
}
// Return CompressWithConfig
return compress(config)
2020-06-06 07:27:26 +02:00
}
// CompressWithConfig is deprecated, please use Compress instead
2020-06-06 07:27:26 +02:00
func CompressWithConfig(config CompressConfig) fiber.Handler {
fmt.Println("compress: `CompressWithConfig()` is deprecated since v1.12.4, please use `Compress()`")
return compress(config)
}
func compress(config CompressConfig) fiber.Handler {
2020-06-06 07:27:26 +02:00
// Init middleware settings
2020-07-04 08:42:42 +02:00
var compressHandler fasthttp.RequestHandler
2020-06-06 07:27:26 +02:00
switch config.Level {
case -1: // Disabled
2020-07-04 08:42:42 +02:00
compressHandler = fasthttp.CompressHandlerBrotliLevel(func(c *fasthttp.RequestCtx) {}, fasthttp.CompressBrotliNoCompression, fasthttp.CompressNoCompression)
2020-06-06 07:27:26 +02:00
case 1: // Best speed
2020-07-04 08:42:42 +02:00
compressHandler = fasthttp.CompressHandlerBrotliLevel(func(c *fasthttp.RequestCtx) {}, fasthttp.CompressBrotliBestSpeed, fasthttp.CompressBestSpeed)
2020-06-06 07:27:26 +02:00
case 2: // Best compression
2020-07-04 08:42:42 +02:00
compressHandler = fasthttp.CompressHandlerBrotliLevel(func(c *fasthttp.RequestCtx) {}, fasthttp.CompressBrotliBestCompression, fasthttp.CompressBestCompression)
2020-06-06 07:27:26 +02:00
default: // Default
2020-07-04 08:42:42 +02:00
compressHandler = fasthttp.CompressHandlerBrotliLevel(func(c *fasthttp.RequestCtx) {}, fasthttp.CompressBrotliDefaultCompression, fasthttp.CompressDefaultCompression)
2020-06-06 07:27:26 +02:00
}
// Return handler
return func(c *fiber.Ctx) {
// Don't execute the middleware if Next returns false
if config.Next != nil && config.Next(c) {
c.Next()
return
}
// Middleware logic...
c.Next()
// Compress response
2020-07-04 08:42:42 +02:00
compressHandler(c.Fasthttp)
2020-06-06 07:27:26 +02:00
}
}