Plugin initial

This commit is contained in:
Piotr Biernat 2022-11-28 12:33:25 +01:00
parent 06134cebc0
commit 51950c49a5
3 changed files with 70 additions and 0 deletions

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module git.pbiernat.dev/traefik/plugin-requestid
go 1.18
require github.com/google/uuid v1.3.0

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

63
requestid.go Normal file
View File

@ -0,0 +1,63 @@
package requestid
import (
"context"
"fmt"
"net/http"
"github.com/google/uuid"
)
const defaultHeaderName = "X-Request-ID"
// Config plugin configuration
type Config struct {
HeaderName string `json:"headerName"`
}
// CreateConfig create default plugin configuration
func CreateConfig() *Config {
return &Config{
HeaderName: defaultHeaderName,
}
}
// RequestIDHeader
type RequestIDHeader struct {
headerName string
name string
next http.Handler
}
// New create new RequestIDHeader
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
hdr := &RequestIDHeader{
next: next,
name: name,
}
if config == nil {
return nil, fmt.Errorf("config can not be nil")
}
if config.HeaderName == "" {
hdr.headerName = defaultHeaderName
} else {
hdr.headerName = config.HeaderName
}
return hdr, nil
}
func (r *RequestIDHeader) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
headerArr := req.Header[r.headerName]
uuid := uuid.New().String()
if len(headerArr) == 0 {
req.Header.Add(r.headerName, uuid)
} else if headerArr[0] == "" {
req.Header[r.headerName][0] = uuid
}
r.next.ServeHTTP(rw, req)
}