New creates a new middleware handler
(config ...Config)
| 57 | |
| 58 | // New creates a new middleware handler |
| 59 | func New(config ...Config) fiber.Handler { |
| 60 | // Set default config |
| 61 | cfg := configDefault(config...) |
| 62 | |
| 63 | // Setup request handlers |
| 64 | var ( |
| 65 | fctx = func(_ *fasthttp.RequestCtx) {} |
| 66 | compressor fasthttp.RequestHandler |
| 67 | ) |
| 68 | |
| 69 | // Setup compression algorithm |
| 70 | switch cfg.Level { |
| 71 | case LevelDefault: |
| 72 | // LevelDefault |
| 73 | compressor = fasthttp.CompressHandlerBrotliLevel( |
| 74 | fctx, |
| 75 | fasthttp.CompressBrotliDefaultCompression, |
| 76 | fasthttp.CompressDefaultCompression, |
| 77 | ) |
| 78 | case LevelBestSpeed: |
| 79 | // LevelBestSpeed |
| 80 | compressor = fasthttp.CompressHandlerBrotliLevel( |
| 81 | fctx, |
| 82 | fasthttp.CompressBrotliBestSpeed, |
| 83 | fasthttp.CompressBestSpeed, |
| 84 | ) |
| 85 | case LevelBestCompression: |
| 86 | // LevelBestCompression |
| 87 | compressor = fasthttp.CompressHandlerBrotliLevel( |
| 88 | fctx, |
| 89 | fasthttp.CompressBrotliBestCompression, |
| 90 | fasthttp.CompressBestCompression, |
| 91 | ) |
| 92 | default: |
| 93 | // LevelDisabled |
| 94 | return func(c fiber.Ctx) error { |
| 95 | return c.Next() |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Return new handler |
| 100 | return func(c fiber.Ctx) error { |
| 101 | // Don't execute middleware if Next returns true |
| 102 | if cfg.Next != nil && cfg.Next(c) { |
| 103 | return c.Next() |
| 104 | } |
| 105 | |
| 106 | // Continue stack |
| 107 | if err := c.Next(); err != nil { |
| 108 | return err |
| 109 | } |
| 110 | |
| 111 | if shouldSkip(c) { |
| 112 | appendVaryAcceptEncoding(c) |
| 113 | return nil |
| 114 | } |
| 115 | |
| 116 | if c.GetRespHeader(fiber.HeaderContentEncoding) != "" { |