优化结构

This commit is contained in:
Yvan
2026-08-28 15:07:16 +08:00
parent ae5345de14
commit a4ed3a02df
15 changed files with 92 additions and 60 deletions
+9 -4
View File
@@ -12,14 +12,16 @@ import (
// OperationAudit consumes these values after the handler returns, avoiding a
// second body read/writer wrapper.
const (
ctxReqBodyKey = "kra_req_body"
ctxRespBufferKey = "kra_resp_buffer"
ctxReqBodyKey = "kra_req_body"
ctxRespBufferKey = "kra_resp_buffer"
ctxRespTruncatedKey = "kra_resp_truncated"
)
type captureWriter struct {
gin.ResponseWriter
body bytes.Buffer
maxBytes int
body bytes.Buffer
maxBytes int
truncated bool
}
func (w *captureWriter) Write(data []byte) (int, error) {
@@ -31,6 +33,7 @@ func (w *captureWriter) Write(data []byte) (int, error) {
remaining := limit - w.body.Len()
if len(data) > remaining {
_, _ = w.body.Write(data[:remaining])
w.truncated = true
} else {
_, _ = w.body.Write(data)
}
@@ -38,6 +41,8 @@ func (w *captureWriter) Write(data []byte) (int, error) {
return w.ResponseWriter.Write(data)
}
func (w *captureWriter) Truncated() bool { return w != nil && w.truncated }
func redactJSON(raw []byte, contentType string, limit int) string {
if len(raw) == 0 {
return ""