RoadSign/pkg/navi/transformers/compress.go

42 lines
969 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
)
2024-01-24 16:09:39 +00:00
var CompressResponse = Transformer{
2023-12-16 03:42:14 +00:00
ModifyResponse: func(options any, ctx *fiber.Ctx) error {
opts := DeserializeOptions[struct {
2024-01-24 16:09:39 +00:00
Level int `json:"level" toml:"level"`
2023-12-16 03:42:14 +00:00
}](options)
2024-01-24 16:09:39 +00:00
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
},
}