RoadSign/pkg/sign/transformers/compress.go

42 lines
979 B
Go
Raw Normal View History

2023-12-16 03:42:14 +00:00
package transformers
import (
"github.com/gofiber/fiber/v2"
"github.com/valyala/fasthttp"
2023-12-16 03:42:14 +00:00
)
var CompressResponse = RequestTransformer{
ModifyResponse: func(options any, ctx *fiber.Ctx) error {
opts := DeserializeOptions[struct {
2023-12-16 14:57:32 +00:00
Level int `json:"level" yaml:"level"`
2023-12-16 03:42:14 +00:00
}](options)
var fctx = func(c *fasthttp.RequestCtx) {}
var compressor fasthttp.RequestHandler
switch opts.Level {
// Best Speed Mode
case 1:
compressor = fasthttp.CompressHandlerBrotliLevel(fctx,
fasthttp.CompressBrotliBestSpeed,
fasthttp.CompressBestSpeed,
)
// Best Compression Mode
case 2:
compressor = fasthttp.CompressHandlerBrotliLevel(fctx,
fasthttp.CompressBrotliBestCompression,
fasthttp.CompressBestCompression,
)
// Default Mode
default:
compressor = fasthttp.CompressHandlerBrotliLevel(fctx,
fasthttp.CompressBrotliDefaultCompression,
fasthttp.CompressDefaultCompression,
)
}
compressor(ctx.Context())
return nil
2023-12-16 03:42:14 +00:00
},
}