RequestID

RequestID middleware for Fiber that adds an indentifier to the response.

Signatures

  1. func New(config ...Config) fiber.Handler

Examples

Import the middleware package that is part of the Fiber web framework

  1. import (
  2. "github.com/gofiber/fiber/v2"
  3. "github.com/gofiber/fiber/v2/middleware/requestid"
  4. )

After you initiate your Fiber app, you can use the following possibilities:

  1. // Default middleware config
  2. app.Use(requestid.New())
  3. // Or extend your config for customization
  4. app.Use(requestid.New(requestid.Config{
  5. Header: "X-Custom-Header",
  6. Generetor: func() string {
  7. return "static-id"
  8. }
  9. }))

Config

  1. // Config defines the config for middleware.
  2. type Config struct {
  3. // Next defines a function to skip this middleware when returned true.
  4. //
  5. // Optional. Default: nil
  6. Next func(c *fiber.Ctx) bool
  7. // Header is the header key where to get/set the unique request ID
  8. //
  9. // Optional. Default: "X-Request-ID"
  10. Header string
  11. // Generator defines a function to generate the unique identifier.
  12. //
  13. // Optional. Default: func() string {
  14. // return utils.UUID()
  15. // }
  16. Generator func() string
  17. }

Default Config

  1. var ConfigDefault = Config{
  2. Next: nil,
  3. Header: fiber.HeaderXRequestID,
  4. Generator: func() string {
  5. return utils.UUID()
  6. },
  7. }