Home / File/ requestid.go — fiber Source File

requestid.go — fiber Source File

Architecture documentation for requestid.go, a go file in the fiber codebase. 1 imports, 0 dependents.

File go FiberCore Adapters 1 imports 4 functions

Entity Profile

Dependency Diagram

graph LR
  45afdaa7_fc57_1e8d_48d9_d278d509e5f7["requestid.go"]
  6604ba6b_bab7_17c7_e687_7d0f07080e5a["v3"]
  45afdaa7_fc57_1e8d_48d9_d278d509e5f7 --> 6604ba6b_bab7_17c7_e687_7d0f07080e5a
  style 45afdaa7_fc57_1e8d_48d9_d278d509e5f7 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

package requestid

import (
	"github.com/gofiber/fiber/v3"
	"github.com/gofiber/utils/v2"
)

// The contextKey type is unexported to prevent collisions with context keys defined in
// other packages.
type contextKey int

// The keys for the values in context
const (
	requestIDKey contextKey = iota
)

// New creates a new middleware handler
func New(config ...Config) fiber.Handler {
	// Set default config
	cfg := configDefault(config...)

	// Return new handler
	return func(c fiber.Ctx) error {
		// Don't execute middleware if Next returns true
		if cfg.Next != nil && cfg.Next(c) {
			return c.Next()
		}
		rid := sanitizeRequestID(c.Get(cfg.Header), cfg.Generator)

		// Set new id to response header
		c.Set(cfg.Header, rid)

		// Add the request ID to locals
		c.Locals(requestIDKey, rid)

		// Continue stack
		return c.Next()
	}
}

// sanitizeRequestID returns the provided request ID when it is valid, otherwise
// it tries up to three values from the configured generator, then falls back to SecureToken.
func sanitizeRequestID(rid string, generator func() string) string {
	if isValidRequestID(rid) {
		return rid
	}

	for range 3 {
		rid = generator()
		if isValidRequestID(rid) {
			return rid
		}
	}

	// Final fallback: SecureToken always produces a valid ID
	return utils.SecureToken()
}

// isValidRequestID reports whether the request ID contains only visible ASCII
// characters (0x20–0x7E) and is non-empty.
func isValidRequestID(rid string) bool {
	if rid == "" {
		return false
	}

	for i := 0; i < len(rid); i++ {
		c := rid[i]
		if c < 0x20 || c > 0x7e {
			return false
		}
	}

	return true
}

// FromContext returns the request ID from context.
// If there is no request ID, an empty string is returned.
func FromContext(c fiber.Ctx) string {
	if rid, ok := c.Locals(requestIDKey).(string); ok {
		return rid
	}
	return ""
}

Domain

Subdomains

Types

Dependencies

  • v3

Frequently Asked Questions

What does requestid.go do?
requestid.go is a source file in the fiber codebase, written in go. It belongs to the FiberCore domain, Adapters subdomain.
What functions are defined in requestid.go?
requestid.go defines 4 function(s): FromContext, New, isValidRequestID, sanitizeRequestID.
What does requestid.go depend on?
requestid.go imports 1 module(s): v3.
Where is requestid.go in the architecture?
requestid.go is located at middleware/requestid/requestid.go (domain: FiberCore, subdomain: Adapters, directory: middleware/requestid).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free