backend requirements onthe xustomer app
This commit is contained in:
33
middlewares/requestid.go
Normal file
33
middlewares/requestid.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// RequestID echoes the caller's X-Request-Id on every response, errors
|
||||
// included, and mints one when the caller did not send it. The customer app
|
||||
// funnels all failures into a single retryable error state, so a support
|
||||
// conversation about "it failed" has nothing to correlate on unless the id the
|
||||
// client already holds comes back on the failing response too.
|
||||
//
|
||||
// Also stashed in c.Locals("requestid") so handlers can log it alongside the
|
||||
// real error they are hiding behind a customer-safe message.
|
||||
func RequestID() fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
id := c.Get("X-Request-Id")
|
||||
if id == "" {
|
||||
b := make([]byte, 8)
|
||||
if _, err := rand.Read(b); err == nil {
|
||||
id = hex.EncodeToString(b)
|
||||
}
|
||||
}
|
||||
if id != "" {
|
||||
c.Locals("requestid", id)
|
||||
c.Set("X-Request-Id", id)
|
||||
}
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user