description
Middleware is a function chained in the HTTP request cycle with access to the Context which it uses to perform a specific action, for example, logging every request or enabling CORS.

🧬 Middleware

Fiber ships with multiple middleware modules by default:

  1. import (
  2. "github.com/gofiber/fiber"
  3. "github.com/gofiber/fiber/middleware"
  4. )
  • ****Compress Compress middleware that supports deflate, gzip and brotli compression.
  • ****FileSystem FileSystem middleware for Fiber, special thanks and credits to Alireza Salary
  • Favicon Ignore favicon from logs or serve from memory if a file path is provided.
  • Logger HTTP request/response logger.
  • Pprof HTTP server runtime profiling
  • Recover Recover middleware recovers from panics anywhere in the stack chain and handles the control to the centralized ErrorHandler.
  • RequestID Request ID middleware generates a unique id for a request.

Fiber also maintains external middleware modules, these have to be installed separately:

  1. import (
  2. "github.com/gofiber/fiber"
  3. "github.com/gofiber/<module>"
  4. )
  • gofiber/adaptor Converter for net/http handlers to/from Fiber request handlers.
  • gofiber/basicauth Basic auth middleware provides an HTTP basic authentication. It calls the next handler for valid credentials and 401 Unauthorized for missing or invalid credentials.
  • gofiber/cors Enable cross-origin resource sharing (CORS) with various options.
  • gofiber/csrf Protect from CSRF exploits.
  • gofiber/helmet Helps secure your apps by setting various HTTP headers.
  • gofiber/jwt JWT returns a JSON Web Token (JWT) auth middleware.
  • gofiber/keyauth Key auth middleware provides a key-based authentication.
  • gofiber/limiter Rate-limiting middleware for Fiber. Use to limit repeated requests to public APIs and/or endpoints such as password reset.
  • gofiber/rewrite Rewrite middleware rewrites the URL path based on provided rules. It can be helpful for backward compatibility or just creating cleaner and more descriptive links.
  • gofiber/session This session middleware is built on top of fasthttp/session by @savsgio MIT. Special thanks to
  • gofiber/template This package contains 8 template engines
  • gofiber/websocket Based on Gorilla WebSocket for Fiber

Compress

Compress middleware for with support for deflate, gzip and brotlicompression.
It will use the fastest compression method depending on the request header Accept-Encodingvalue.

  1. func Compress(options ...interface{}) fiber.Handler {}
  1. type CompressConfig struct {
  2. // Next defines a function to skip this middleware.
  3. // Default: nil
  4. Next func(*fiber.Ctx) bool
  5. // Compression level for brotli, gzip and deflate
  6. // CompressLevelDisabled = -1
  7. // CompressLevelDefault = 0
  8. // CompressLevelBestSpeed = 1
  9. // CompressLevelBestCompression = 2
  10. // Default: CompressLevelDefault
  11. Level int
  12. }
  1. // Compression handler with default settings
  2. app.Use(middleware.Compress())
  3. // Provide a custom compression level
  4. app.Use(middleware.Compress(2))
  5. // Pass a next function to skip specific requests
  6. app.Use(middleware.Compress(func(c *fiber.Ctx) bool {
  7. return c.Path() == "/dontcompress"
  8. }))
  9. // Provide a full Config
  10. app.Use(middleware.Compress(middleware.CompressConfig{
  11. Next: func(c *fiber.Ctx) bool {
  12. return c.Path() == "/dontcompress"
  13. },
  14. Level: CompressLevelDefault,
  15. })

FileSystem

Favicon