-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
151 lines (122 loc) · 3.32 KB
/
main.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package traefik_plugin_log_request
import (
"bufio"
"bytes"
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
)
// Config holds the plugin configuration.
type Config struct {
ResponseBody bool `json:"responseBody,omitempty"`
MaxLineSize int `json:"maxLineSize,omitempty"`
RequestIDHeaderName string `json:"requestIDHeaderName,omitempty"`
}
// CreateConfig creates and initializes the plugin configuration.
func CreateConfig() *Config {
return &Config{}
}
type logRequest struct {
name string
next http.Handler
responseBody bool
requestIDHeaderName string
maxLineSize int
}
type RequestData struct {
URL string `json:"url"`
Host string `json:"host"`
Body string `json:"body"`
Headers string `json:"headers"`
ResponseBody string `json:"response_body"`
RequestID string `json:"request_id"`
}
func New(_ context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
if config.RequestIDHeaderName == "" {
config.RequestIDHeaderName = "X-Request-Id"
}
return &logRequest{
name: name,
next: next,
responseBody: config.ResponseBody,
requestIDHeaderName: config.RequestIDHeaderName,
maxLineSize: config.MaxLineSize,
}, nil
}
func (p *logRequest) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
requestId, _ := generateRandomID(16)
if req.Header.Get(p.requestIDHeaderName) != "" {
requestId = req.Header.Get(p.requestIDHeaderName)
}
req.Header.Set(p.requestIDHeaderName, requestId)
body, err := io.ReadAll(req.Body)
if err != nil {
}
req.Body = io.NopCloser(bytes.NewBuffer(body))
wrappedWriter := &responseWriter{
ResponseWriter: rw,
}
p.next.ServeHTTP(wrappedWriter, req)
bodyBytes := wrappedWriter.buffer.Bytes()
rw.Write(bodyBytes)
headers := make(map[string]string)
for name, values := range req.Header {
headers[name] = values[0] // Take the first value of the header
}
jsonHeader, err := json.Marshal(headers)
if err != nil {
}
requestData := RequestData{
URL: req.URL.String(),
Host: req.Host,
Body: string(body),
Headers: string(jsonHeader),
RequestID: requestId,
}
if p.responseBody {
responseBody := io.NopCloser(bytes.NewBuffer(bodyBytes))
responseBodyBytes, err := io.ReadAll(responseBody)
if err != nil {
// ignore
}
if len(responseBodyBytes) < p.maxLineSize {
requestData.ResponseBody = string(responseBodyBytes)
}
}
jsonData, err := json.Marshal(requestData)
if err != nil {
}
os.Stdout.WriteString(string(jsonData) + "\n")
}
type responseWriter struct {
buffer bytes.Buffer
http.ResponseWriter
}
func (r *responseWriter) Write(p []byte) (int, error) {
return r.buffer.Write(p)
}
func (r *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hijacker, ok := r.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, fmt.Errorf("%T is not a http.Hijacker", r.ResponseWriter)
}
return hijacker.Hijack()
}
func (r *responseWriter) Flush() {
if flusher, ok := r.ResponseWriter.(http.Flusher); ok {
flusher.Flush()
}
}
func generateRandomID(length int) (string, error) {
bytes := make([]byte, length)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}