2024-05-23 14:44:31 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
|
|
|
|
|
2017-06-05 15:01:05 -07:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2019-01-31 08:06:36 -08:00
|
|
|
"github.com/labstack/echo/v4"
|
2017-06-05 15:01:05 -07:00
|
|
|
)
|
|
|
|
|
2024-05-23 14:44:31 -07:00
|
|
|
// RequestIDConfig defines the config for RequestID middleware.
|
|
|
|
type RequestIDConfig struct {
|
|
|
|
// Skipper defines a function to skip middleware.
|
|
|
|
Skipper Skipper
|
2017-06-05 15:01:05 -07:00
|
|
|
|
2024-05-23 14:44:31 -07:00
|
|
|
// Generator defines a function to generate an ID.
|
|
|
|
// Optional. Defaults to generator for random string of length 32.
|
|
|
|
Generator func() string
|
2021-07-31 09:27:55 -07:00
|
|
|
|
2024-05-23 14:44:31 -07:00
|
|
|
// RequestIDHandler defines a function which is executed for a request id.
|
|
|
|
RequestIDHandler func(echo.Context, string)
|
2022-01-18 11:42:25 -08:00
|
|
|
|
2024-05-23 14:44:31 -07:00
|
|
|
// TargetHeader defines what header to look for to populate the id
|
|
|
|
TargetHeader string
|
|
|
|
}
|
2017-06-05 15:01:05 -07:00
|
|
|
|
2024-05-23 14:44:31 -07:00
|
|
|
// DefaultRequestIDConfig is the default RequestID middleware config.
|
|
|
|
var DefaultRequestIDConfig = RequestIDConfig{
|
|
|
|
Skipper: DefaultSkipper,
|
|
|
|
Generator: generator,
|
|
|
|
TargetHeader: echo.HeaderXRequestID,
|
|
|
|
}
|
2017-06-05 15:01:05 -07:00
|
|
|
|
|
|
|
// RequestID returns a X-Request-ID middleware.
|
|
|
|
func RequestID() echo.MiddlewareFunc {
|
|
|
|
return RequestIDWithConfig(DefaultRequestIDConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequestIDWithConfig returns a X-Request-ID middleware with config.
|
|
|
|
func RequestIDWithConfig(config RequestIDConfig) echo.MiddlewareFunc {
|
|
|
|
// Defaults
|
|
|
|
if config.Skipper == nil {
|
|
|
|
config.Skipper = DefaultRequestIDConfig.Skipper
|
|
|
|
}
|
|
|
|
if config.Generator == nil {
|
|
|
|
config.Generator = generator
|
|
|
|
}
|
2022-01-18 11:42:25 -08:00
|
|
|
if config.TargetHeader == "" {
|
|
|
|
config.TargetHeader = echo.HeaderXRequestID
|
|
|
|
}
|
2017-06-05 15:01:05 -07:00
|
|
|
|
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
if config.Skipper(c) {
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := c.Request()
|
|
|
|
res := c.Response()
|
2022-01-18 11:42:25 -08:00
|
|
|
rid := req.Header.Get(config.TargetHeader)
|
2017-06-05 15:01:05 -07:00
|
|
|
if rid == "" {
|
|
|
|
rid = config.Generator()
|
|
|
|
}
|
2022-01-18 11:42:25 -08:00
|
|
|
res.Header().Set(config.TargetHeader, rid)
|
2021-07-31 09:27:55 -07:00
|
|
|
if config.RequestIDHandler != nil {
|
|
|
|
config.RequestIDHandler(c, rid)
|
|
|
|
}
|
2017-06-05 15:01:05 -07:00
|
|
|
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func generator() string {
|
2024-05-23 14:44:31 -07:00
|
|
|
return randomString(32)
|
2017-06-05 15:01:05 -07:00
|
|
|
}
|