MCPcopy
hub / github.com/gofiber/fiber / New

Function New

middleware/compress/compress.go:59–133  ·  view source on GitHub ↗

New creates a new middleware handler

(config ...Config)

Source from the content-addressed store, hash-verified

57
58// New creates a new middleware handler
59func 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) != "" {

Calls 10

GenerateFunction · 0.92
shouldSkipFunction · 0.85
appendVaryAcceptEncodingFunction · 0.85
configDefaultFunction · 0.70
NextMethod · 0.65
GetRespHeaderMethod · 0.65
RequestCtxMethod · 0.65
SetMethod · 0.65
BodyMethod · 0.65
ResponseMethod · 0.65