From 51950c49a5866a7086464fdb256f0bda5516a697 Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Mon, 28 Nov 2022 12:33:25 +0100 Subject: [PATCH] Plugin initial --- go.mod | 5 +++++ go.sum | 2 ++ requestid.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 requestid.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4fbabb3 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.pbiernat.dev/traefik/plugin-requestid + +go 1.18 + +require github.com/google/uuid v1.3.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3dfe1c9 --- /dev/null +++ b/go.sum @@ -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= diff --git a/requestid.go b/requestid.go new file mode 100644 index 0000000..0123f0c --- /dev/null +++ b/requestid.go @@ -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) +}