-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
request_id.go
105 lines (95 loc) · 2.64 KB
/
request_id.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package middleware
import (
"fmt"
"log"
fiber "github.com/gofiber/fiber"
utils "github.com/gofiber/utils"
)
// Middleware types
type (
// RequestIDConfig defines the config for Logger middleware.
RequestIDConfig struct {
// Next defines a function to skip this middleware.
Next func(ctx *fiber.Ctx) bool
// Header is the header key where to get/set the unique ID
// Optional. Default: X-Request-ID
Header string
// Generator defines a function to generate the unique identifier.
// Optional. Default: func() string {
// return utils.UUID()
// }
Generator func() string
}
)
// RequestIDConfigDefault is the default config
var RequestIDConfigDefault = RequestIDConfig{
Next: nil,
Header: fiber.HeaderXRequestID,
Generator: func() string {
return utils.UUID()
},
}
// RequestID adds an UUID indentifier to the request
/*
RequestID adds an UUID indentifier to the request, the following config arguments in any order:
- RequestID()
- RequestID(next func(*fiber.Ctx) bool)
- RequestID(header string)
- RequestID(generator func() string)
- RequestID(config RequestIDConfig)
*/
func RequestID(options ...interface{}) fiber.Handler {
// Create default config
var config = RequestIDConfigDefault
// Assert options if provided to adjust the config
if len(options) > 0 {
for i := range options {
switch opt := options[i].(type) {
case func(*fiber.Ctx) bool:
config.Next = opt
case string:
config.Header = opt
case func() string:
config.Generator = opt
case RequestIDConfig:
config = opt
default:
log.Fatal("RequestID: the following option types are allowed: `string`, `func() string`, `func(*fiber.Ctx) bool`, `RequestIDConfig`")
}
}
}
// Return requestID
return requestID(config)
}
// RequestIDWithConfig is deprecated, please use RequestID instead
func RequestIDWithConfig(config RequestIDConfig) fiber.Handler {
fmt.Println("compress: `RequestIDWithConfig()` is deprecated since v1.12.4, please use `RequestID()`")
return requestID(config)
}
func requestID(config RequestIDConfig) fiber.Handler {
// Set default values
if config.Header == "" {
config.Header = RequestIDConfigDefault.Header
}
if config.Generator == nil {
config.Generator = RequestIDConfigDefault.Generator
}
// Return handler
return func(ctx *fiber.Ctx) {
// Don't execute the middleware if Next returns true
if config.Next != nil && config.Next(ctx) {
ctx.Next()
return
}
// Get id from request
rid := ctx.Get(config.Header)
// Create new UUID if empty
if len(rid) <= 0 {
rid = config.Generator()
}
// Set new id to response header
ctx.Set(config.Header, rid)
// Continue stack
ctx.Next()
}
}