Merge branch 'main' of https://github.com/samber/slog-gin
This commit is contained in:
@@ -8,18 +8,24 @@ import (
|
|||||||
|
|
||||||
type bodyWriter struct {
|
type bodyWriter struct {
|
||||||
gin.ResponseWriter
|
gin.ResponseWriter
|
||||||
body *bytes.Buffer
|
body *bytes.Buffer
|
||||||
|
maxSize int
|
||||||
}
|
}
|
||||||
|
|
||||||
// implements gin.ResponseWriter
|
// implements gin.ResponseWriter
|
||||||
func (w bodyWriter) Write(b []byte) (int, error) {
|
func (w bodyWriter) Write(b []byte) (int, error) {
|
||||||
w.body.Write(b)
|
if w.body.Len()+len(b) > w.maxSize {
|
||||||
|
w.body.Write(b[:w.maxSize-w.body.Len()])
|
||||||
|
} else {
|
||||||
|
w.body.Write(b)
|
||||||
|
}
|
||||||
return w.ResponseWriter.Write(b)
|
return w.ResponseWriter.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newBodyWriter(writer gin.ResponseWriter) *bodyWriter {
|
func newBodyWriter(writer gin.ResponseWriter, maxSize int) *bodyWriter {
|
||||||
return &bodyWriter{
|
return &bodyWriter{
|
||||||
body: bytes.NewBufferString(""),
|
body: bytes.NewBufferString(""),
|
||||||
ResponseWriter: writer,
|
ResponseWriter: writer,
|
||||||
|
maxSize: maxSize,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-2
@@ -20,6 +20,9 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
RequestBodyMaxSize = 64 * 1024 // 64KB
|
||||||
|
ResponseBodyMaxSize = 64 * 1024 // 64KB
|
||||||
|
|
||||||
HiddenRequestHeaders = map[string]struct{}{
|
HiddenRequestHeaders = map[string]struct{}{
|
||||||
"authorization": {},
|
"authorization": {},
|
||||||
"cookie": {},
|
"cookie": {},
|
||||||
@@ -105,13 +108,17 @@ func NewWithConfig(logger *slog.Logger, config Config) gin.HandlerFunc {
|
|||||||
buf, err := io.ReadAll(c.Request.Body)
|
buf, err := io.ReadAll(c.Request.Body)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
c.Request.Body = io.NopCloser(bytes.NewBuffer(buf))
|
c.Request.Body = io.NopCloser(bytes.NewBuffer(buf))
|
||||||
reqBody = buf
|
if len(buf) > RequestBodyMaxSize {
|
||||||
|
reqBody = buf[:RequestBodyMaxSize]
|
||||||
|
} else {
|
||||||
|
reqBody = buf
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// dump response body
|
// dump response body
|
||||||
if config.WithResponseBody {
|
if config.WithResponseBody {
|
||||||
c.Writer = newBodyWriter(c.Writer)
|
c.Writer = newBodyWriter(c.Writer, ResponseBodyMaxSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|||||||
Reference in New Issue
Block a user