generated from traefik/plugindemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequestid.go
40 lines (32 loc) · 837 Bytes
/
requestid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Package traefik_request_id a Traefik plugin to add request ID to incoming HTTP requests.
package traefik_request_id
import (
"context"
"net/http"
"github.com/google/uuid"
)
// Config the plugin configuration.
type Config struct{}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{}
}
// Plugin a request id plugin.
type Plugin struct {
next http.Handler
name string
}
// New created a new Plugin plugin.
func New(_ context.Context, next http.Handler, _ *Config, name string) (http.Handler, error) {
return &Plugin{
next: next,
name: name,
}, nil
}
func (a *Plugin) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
cid := req.Header.Get("X-Request-ID")
if cid == "" {
req.Header.Set("X-Request-ID", uuid.New().String())
}
a.next.ServeHTTP(rw, req)
}