优化结构

This commit is contained in:
Yvan
2026-08-28 15:37:40 +08:00
parent a4ed3a02df
commit 9b1daaf3f6
17 changed files with 100 additions and 64 deletions
+2 -1
View File
@@ -97,7 +97,6 @@ func AccessLog(runtime *config.Store, logger *slog.Logger, version string) gin.H
requestText = redactJSON(requestBody, c.GetHeader("Content-Type"), logLimit)
}
c.Set(ctxReqBodyKey, requestText)
c.Set(ctxRespTruncatedKey, writer.Truncated())
if paymentCallback {
// Callback acknowledgements and provider payloads must not flow into
// the generic response/error audit pipeline.
@@ -108,6 +107,7 @@ func AccessLog(runtime *config.Store, logger *slog.Logger, version string) gin.H
if !requestReadFailed {
c.Next()
}
c.Set(ctxRespTruncatedKey, writer.Truncated())
if logger == nil {
return
}
@@ -118,6 +118,7 @@ func AccessLog(runtime *config.Store, logger *slog.Logger, version string) gin.H
if paymentCallback {
responseText = "[支付回调响应已省略]"
}
c.Set(ctxRespTextKey, responseText)
userID, authorityID := uint(0), uint(0)
if claims := Claims(c); claims != nil {
userID, authorityID = claims.ID, claims.AuthorityID
+5 -4
View File
@@ -73,7 +73,9 @@ func OperationAudit(runtime *config.Store, recorder *systemservice.AuditRecorder
requestID, _ := c.Get("request_id")
status := c.Writer.Status()
responseBody := ""
if value, ok := c.Get(ctxRespBufferKey); ok {
if value, ok := c.Get(ctxRespTextKey); ok {
responseBody = stringValue(value)
} else if value, ok := c.Get(ctxRespBufferKey); ok {
if body, bok := value.(*bytes.Buffer); bok {
responseBody = redactJSON(body.Bytes(), c.Writer.Header().Get("Content-Type"), maxBytes)
}
@@ -91,10 +93,9 @@ func OperationAudit(runtime *config.Store, recorder *systemservice.AuditRecorder
// second time.
operationBody = stringValue(value)
capturedBody = true
} else {
operationBody = operationRequestBody(requestBody, c.GetHeader("Content-Type"), maxBytes)
}
} else {
}
if !capturedBody {
operationBody = operationRequestBody(requestBody, c.GetHeader("Content-Type"), maxBytes)
}
if !capturedBody && routecatalog.BodyPolicyFor(c.Request.Method, path) == routecatalog.BodyPolicyPaymentConfig {
+3
View File
@@ -15,6 +15,7 @@ const (
ctxReqBodyKey = "kra_req_body"
ctxRespBufferKey = "kra_resp_buffer"
ctxRespTruncatedKey = "kra_resp_truncated"
ctxRespTextKey = "kra_resp_text"
)
type captureWriter struct {
@@ -37,6 +38,8 @@ func (w *captureWriter) Write(data []byte) (int, error) {
} else {
_, _ = w.body.Write(data)
}
} else if len(data) > 0 {
w.truncated = true
}
return w.ResponseWriter.Write(data)
}
+3 -1
View File
@@ -22,7 +22,9 @@ func ErrorAudit(logger *slog.Logger) gin.HandlerFunc {
// already recorded by the handler with the underlying filesystem error;
// emitting again from the response envelope would duplicate both the
// classified error file and the sys_error row.
if auditPersistFailed != true && (strings.Contains(c.Request.URL.Path, "/sysError/") || strings.Contains(c.Request.URL.Path, "/logViewer/") || c.Writer.Status() >= 300 && c.Writer.Status() < 500) {
isSelfAudit := strings.Contains(c.Request.URL.Path, "/sysError/") || strings.Contains(c.Request.URL.Path, "/logViewer/")
isClientFailure := c.Writer.Status() >= 300 && c.Writer.Status() < 500
if auditPersistFailed != true && (isSelfAudit || isClientFailure) {
return
}
var response httpx.Response
+1 -1
View File
@@ -45,7 +45,7 @@ func SecurityRateLimit(settings *systemservice.SecurityService) gin.HandlerFunc
return
}
if int(count) > config.LimitCount {
c.JSON(200, gin.H{"code": httpx.CodeError, "msg": "请求太过频繁,请稍后再试"})
c.JSON(http.StatusOK, gin.H{"code": httpx.CodeError, "msg": "请求太过频繁,请稍后再试"})
c.Abort()
return
}