mirror of https://github.com/gofiber/fiber.git
Merge bd6de23a02
into bb12633c8b
commit
503866dada
|
@ -49,16 +49,6 @@ func handler(c fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
In version v3, Fiber will inject `requestID` into the built-in `Context` of Go.
|
|
||||||
|
|
||||||
```go
|
|
||||||
func handler(c fiber.Ctx) error {
|
|
||||||
id := requestid.FromContext(c.Context())
|
|
||||||
log.Printf("Request ID: %s", id)
|
|
||||||
return c.SendString("Hello, World!")
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Config
|
## Config
|
||||||
|
|
||||||
| Property | Type | Description | Default |
|
| Property | Type | Description | Default |
|
||||||
|
|
|
@ -1,10 +1,7 @@
|
||||||
package requestid
|
package requestid
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v3"
|
"github.com/gofiber/fiber/v3"
|
||||||
"github.com/gofiber/fiber/v3/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// The contextKey type is unexported to prevent collisions with context keys defined in
|
// The contextKey type is unexported to prevent collisions with context keys defined in
|
||||||
|
@ -39,10 +36,6 @@ func New(config ...Config) fiber.Handler {
|
||||||
// Add the request ID to locals
|
// Add the request ID to locals
|
||||||
c.Locals(requestIDKey, rid)
|
c.Locals(requestIDKey, rid)
|
||||||
|
|
||||||
// Add the request ID to UserContext
|
|
||||||
ctx := context.WithValue(c.Context(), requestIDKey, rid)
|
|
||||||
c.SetContext(ctx)
|
|
||||||
|
|
||||||
// Continue stack
|
// Continue stack
|
||||||
return c.Next()
|
return c.Next()
|
||||||
}
|
}
|
||||||
|
@ -50,21 +43,9 @@ func New(config ...Config) fiber.Handler {
|
||||||
|
|
||||||
// FromContext returns the request ID from context.
|
// FromContext returns the request ID from context.
|
||||||
// If there is no request ID, an empty string is returned.
|
// If there is no request ID, an empty string is returned.
|
||||||
// Supported context types:
|
func FromContext(c fiber.Ctx) string {
|
||||||
// - fiber.Ctx: Retrieves request ID from Locals
|
if rid, ok := c.Locals(requestIDKey).(string); ok {
|
||||||
// - context.Context: Retrieves request ID from context values
|
return rid
|
||||||
func FromContext(c any) string {
|
|
||||||
switch ctx := c.(type) {
|
|
||||||
case fiber.Ctx:
|
|
||||||
if rid, ok := ctx.Locals(requestIDKey).(string); ok {
|
|
||||||
return rid
|
|
||||||
}
|
|
||||||
case context.Context:
|
|
||||||
if rid, ok := ctx.Value(requestIDKey).(string); ok {
|
|
||||||
return rid
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
log.Errorf("Unsupported context type: %T. Expected fiber.Ctx or context.Context", c)
|
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,59 +51,26 @@ func Test_RequestID_Next(t *testing.T) {
|
||||||
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
|
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
// go test -run Test_RequestID_FromContext
|
// go test -run Test_RequestID_Locals
|
||||||
func Test_RequestID_FromContext(t *testing.T) {
|
func Test_RequestID_FromContext(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
reqID := "ThisIsARequestId"
|
reqID := "ThisIsARequestId"
|
||||||
|
|
||||||
type args struct {
|
app := fiber.New()
|
||||||
inputFunc func(c fiber.Ctx) any
|
app.Use(New(Config{
|
||||||
}
|
Generator: func() string {
|
||||||
|
return reqID
|
||||||
tests := []struct {
|
|
||||||
args args
|
|
||||||
name string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "From fiber.Ctx",
|
|
||||||
args: args{
|
|
||||||
inputFunc: func(c fiber.Ctx) any {
|
|
||||||
return c
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
}))
|
||||||
name: "From context.Context",
|
|
||||||
args: args{
|
|
||||||
inputFunc: func(c fiber.Ctx) any {
|
|
||||||
return c.Context()
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
var ctxVal string
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
app := fiber.New()
|
app.Use(func(c fiber.Ctx) error {
|
||||||
app.Use(New(Config{
|
ctxVal = FromContext(c)
|
||||||
Generator: func() string {
|
return c.Next()
|
||||||
return reqID
|
})
|
||||||
},
|
|
||||||
}))
|
|
||||||
|
|
||||||
var ctxVal string
|
_, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
|
||||||
|
require.NoError(t, err)
|
||||||
app.Use(func(c fiber.Ctx) error {
|
require.Equal(t, reqID, ctxVal)
|
||||||
ctxVal = FromContext(tt.args.inputFunc(c))
|
|
||||||
return c.Next()
|
|
||||||
})
|
|
||||||
|
|
||||||
_, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, reqID, ctxVal)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue