mirror of
https://github.com/cwinfo/matterbridge.git
synced 2024-11-14 06:10:27 +00:00
61d56f26f8
Bumps [github.com/labstack/echo/v4](https://github.com/labstack/echo) from 4.6.1 to 4.6.3. - [Release notes](https://github.com/labstack/echo/releases) - [Changelog](https://github.com/labstack/echo/blob/master/CHANGELOG.md) - [Commits](https://github.com/labstack/echo/compare/v4.6.1...v4.6.3) --- updated-dependencies: - dependency-name: github.com/labstack/echo/v4 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
package middleware
|
|
|
|
import (
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/labstack/gommon/random"
|
|
)
|
|
|
|
type (
|
|
// RequestIDConfig defines the config for RequestID middleware.
|
|
RequestIDConfig struct {
|
|
// Skipper defines a function to skip middleware.
|
|
Skipper Skipper
|
|
|
|
// Generator defines a function to generate an ID.
|
|
// Optional. Default value random.String(32).
|
|
Generator func() string
|
|
|
|
// RequestIDHandler defines a function which is executed for a request id.
|
|
RequestIDHandler func(echo.Context, string)
|
|
|
|
// TargetHeader defines what header to look for to populate the id
|
|
TargetHeader string
|
|
}
|
|
)
|
|
|
|
var (
|
|
// DefaultRequestIDConfig is the default RequestID middleware config.
|
|
DefaultRequestIDConfig = RequestIDConfig{
|
|
Skipper: DefaultSkipper,
|
|
Generator: generator,
|
|
TargetHeader: echo.HeaderXRequestID,
|
|
}
|
|
)
|
|
|
|
// 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
|
|
}
|
|
if config.TargetHeader == "" {
|
|
config.TargetHeader = echo.HeaderXRequestID
|
|
}
|
|
|
|
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()
|
|
rid := req.Header.Get(config.TargetHeader)
|
|
if rid == "" {
|
|
rid = config.Generator()
|
|
}
|
|
res.Header().Set(config.TargetHeader, rid)
|
|
if config.RequestIDHandler != nil {
|
|
config.RequestIDHandler(c, rid)
|
|
}
|
|
|
|
return next(c)
|
|
}
|
|
}
|
|
}
|
|
|
|
func generator() string {
|
|
return random.String(32)
|
|
}
|