优化结构
This commit is contained in:
@@ -65,7 +65,7 @@ func NewGinEngineWithRuntime(runtime *config.Store, access *systemservice.Access
|
||||
if snapshot != nil && snapshot.Admin != nil && snapshot.Admin.App != nil {
|
||||
env = strings.ToLower(strings.TrimSpace(snapshot.Admin.App.Env))
|
||||
}
|
||||
if env != "production" && env != "prod" && env != "live" && env != "staging" {
|
||||
if env == "" || env == "development" || env == "dev" || env == "test" || env == "local" {
|
||||
registerSwagger(engine, prefix, version, logger)
|
||||
}
|
||||
staticfiles.Register(engine, runtime)
|
||||
|
||||
@@ -53,6 +53,11 @@ func sanitizeFailureMessage(message string) string {
|
||||
if strings.Contains(message, "失败:") {
|
||||
return strings.TrimSpace(strings.SplitN(message, "失败:", 2)[0]) + "失败"
|
||||
}
|
||||
for _, marker := range []string{"参数", "不能为空", "无效", "不存在", "权限", "未登录", "验证码", "密码", "重复", "不支持", "超出", "分片", "上传", "文件", "名称", "令牌", "token"} {
|
||||
if strings.Contains(message, marker) {
|
||||
return message
|
||||
}
|
||||
}
|
||||
return message
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user