mirror of https://github.com/gofiber/fiber.git
Update
parent
15b0630ede
commit
22555c0806
2
app.go
2
app.go
|
@ -558,7 +558,7 @@ func (app *App) init() *App {
|
||||||
} else {
|
} else {
|
||||||
ctx.err = ErrBadRequest
|
ctx.err = ErrBadRequest
|
||||||
}
|
}
|
||||||
app.Settings.ErrorHandler(ctx, ctx.err) // ctx.Route() not available
|
app.Settings.ErrorHandler(ctx, ctx.err)
|
||||||
app.ReleaseCtx(ctx)
|
app.ReleaseCtx(ctx)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
8
ctx.go
8
ctx.go
|
@ -777,6 +777,14 @@ func (ctx *Ctx) Render(name string, bind interface{}) (err error) {
|
||||||
|
|
||||||
// Route returns the matched Route struct.
|
// Route returns the matched Route struct.
|
||||||
func (ctx *Ctx) Route() *Route {
|
func (ctx *Ctx) Route() *Route {
|
||||||
|
if ctx.route == nil {
|
||||||
|
// Fallback for fasthttp error handler
|
||||||
|
return &Route{
|
||||||
|
Path: ctx.pathOriginal,
|
||||||
|
Method: ctx.method,
|
||||||
|
Handlers: make([]Handler, 0),
|
||||||
|
}
|
||||||
|
}
|
||||||
return ctx.route
|
return ctx.route
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
# Request ID
|
||||||
|
|
||||||
|
Adds an indentifier to the response using the `X-Request-ID` header
|
||||||
|
|
||||||
|
|
||||||
|
### Signatures
|
||||||
|
```go
|
||||||
|
func RequestID(header ...string) fiber.Handler {}
|
||||||
|
func RequestIDWithConfig(config RequestIDConfig) fiber.Handler {}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Config
|
||||||
|
```go
|
||||||
|
type 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
|
||||||
|
// Optiona. Defaults: X-Request-ID
|
||||||
|
Header string
|
||||||
|
|
||||||
|
// Generator defines a function to generate the unique identifier.
|
||||||
|
// Optional. Default: func() string {
|
||||||
|
// return utils.UUID()
|
||||||
|
// }
|
||||||
|
Generator func() string
|
||||||
|
}
|
||||||
|
```
|
||||||
|
### Default Config
|
||||||
|
```go
|
||||||
|
var RequestIDConfigDefault = RequestIDConfig{
|
||||||
|
Next: nil,
|
||||||
|
Header: fiber.HeaderXRequestID,
|
||||||
|
Generator: func() string {
|
||||||
|
return utils.UUID()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
### Example
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gofiber/fiber"
|
||||||
|
"github.com/gofiber/middleware"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
app := fiber.New()
|
||||||
|
|
||||||
|
// Default
|
||||||
|
app.Use(middleware.RequestID())
|
||||||
|
|
||||||
|
// Custom Header
|
||||||
|
app.Use(middleware.RequestID("X-Custom-Header"))
|
||||||
|
|
||||||
|
// Custom Config
|
||||||
|
app.Use(middleware.RequestID(middleware.RequestIDConfig{
|
||||||
|
Next: func(ctx *fiber.Ctx) bool {
|
||||||
|
return ctx.Method() != fiber.MethodPost
|
||||||
|
},
|
||||||
|
Header: "X-Custom-Header",
|
||||||
|
Generator: func() string {
|
||||||
|
return "1234567890"
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
app.Listen(3000)
|
||||||
|
}
|
||||||
|
```
|
Loading…
Reference in New Issue