mirror of
https://github.com/labstack/echo.git
synced 2024-12-18 16:20:53 +02:00
6d9e043284
This reintroduces support for Go modules, as v4. CloseNotifier() is removed as it has been obsoleted, see https://golang.org/doc/go1.11#net/http It was already NOT working (not sending signals) as of 1.11 the functionality was gone, we merely deleted the functions that exposed it. If anyone still relies on it they should migrate to using `c.Request().Context().Done()` instead. Closes #1268, #1255
65 lines
1.4 KiB
Go
65 lines
1.4 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
|
|
}
|
|
)
|
|
|
|
var (
|
|
// DefaultRequestIDConfig is the default RequestID middleware config.
|
|
DefaultRequestIDConfig = RequestIDConfig{
|
|
Skipper: DefaultSkipper,
|
|
Generator: generator,
|
|
}
|
|
)
|
|
|
|
// 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
|
|
}
|
|
|
|
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(echo.HeaderXRequestID)
|
|
if rid == "" {
|
|
rid = config.Generator()
|
|
}
|
|
res.Header().Set(echo.HeaderXRequestID, rid)
|
|
|
|
return next(c)
|
|
}
|
|
}
|
|
}
|
|
|
|
func generator() string {
|
|
return random.String(32)
|
|
}
|