-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgin_response_writer.go
119 lines (100 loc) · 2.78 KB
/
gin_response_writer.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
package moesifgin
import (
"bufio"
"bytes"
"log"
"net"
"net/http"
"github.com/gin-gonic/gin"
)
const (
noWritten = -1
defaultStatus = http.StatusOK
)
// This wraps the gin.ResponseWriter to capture the response body for logging
type logGinResponseWriter struct {
gin.ResponseWriter
body *bytes.Buffer
size int
status int
}
var _ gin.ResponseWriter = (*logGinResponseWriter)(nil) // Ensure that it implements the interface completely
func NewLogGinResponseWriter(w gin.ResponseWriter) *logGinResponseWriter {
return &logGinResponseWriter{
ResponseWriter: w,
body: new(bytes.Buffer),
size: noWritten,
status: defaultStatus,
}
}
func (w *logGinResponseWriter) WriteHeader(code int) {
if w.status != code {
if w.Written() {
log.Printf("[WARNING] Headers were already written. Wanted to override status code %d with %d", w.status, code)
return
}
w.status = code
w.ResponseWriter.WriteHeader(code)
}
}
func (w *logGinResponseWriter) Write(data []byte) (int, error) {
w.WriteHeaderNow() // Ensure header is written if it's not already
n, err := w.body.Write(data) // Write to the buffer first
if err != nil {
return n, err
}
return w.ResponseWriter.Write(data) // Write to the underlying ResponseWriter
}
func (w *logGinResponseWriter) WriteString(s string) (int, error) {
w.WriteHeaderNow()
n, err := w.body.WriteString(s) // Write string to the buffer
if err != nil {
return n, err
}
return w.ResponseWriter.Write([]byte(s)) // Convert string to bytes and write
}
func (w *logGinResponseWriter) Body() *bytes.Buffer {
return w.body
}
func (w *logGinResponseWriter) Flush() {
w.WriteHeaderNow()
w.ResponseWriter.(http.Flusher).Flush()
if w.body.Len() > 0 {
log.Printf("Buffered response body: %s", w.body.String()) // Log or process the buffered response body
}
}
func (w *logGinResponseWriter) Unwrap() http.ResponseWriter {
return w.ResponseWriter
}
func (w *logGinResponseWriter) WriteHeaderNow() {
if !w.Written() {
w.size = 0
w.ResponseWriter.WriteHeader(w.status)
}
}
func (w *logGinResponseWriter) Status() int {
return w.status
}
func (w *logGinResponseWriter) Size() int {
return w.size
}
func (w *logGinResponseWriter) Written() bool {
return w.size != noWritten
}
// Hijack implements the http.Hijacker interface.
func (w *logGinResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if w.size < 0 {
w.size = 0
}
return w.ResponseWriter.(http.Hijacker).Hijack()
}
// CloseNotify implements the http.CloseNotifier interface.
func (w *logGinResponseWriter) CloseNotify() <-chan bool {
return w.ResponseWriter.(http.CloseNotifier).CloseNotify()
}
func (w *logGinResponseWriter) Pusher() (pusher http.Pusher) {
if pusher, ok := w.ResponseWriter.(http.Pusher); ok {
return pusher
}
return nil
}