From b3d9123d59850e6d31f1b873b2feeac33079f3c9 Mon Sep 17 00:00:00 2001 From: mamba <371510756@qq.com> Date: Mon, 13 Jan 2025 16:24:51 +0800 Subject: [PATCH] =?UTF-8?q?[frontend-gray]=20=E5=BE=AE=E5=89=8D=E7=AB=AF?= =?UTF-8?q?=E7=81=B0=E5=BA=A6=20=E5=9C=BA=E6=99=AF,=E6=94=AF=E6=8C=81=20In?= =?UTF-8?q?cludePathPrefixes=E5=AD=97=E6=AE=B5=20(#1666)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/wasm-go/extensions/frontend-gray/README.md | 1 + plugins/wasm-go/extensions/frontend-gray/config/config.go | 2 ++ plugins/wasm-go/extensions/frontend-gray/util/utils.go | 8 +++++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/plugins/wasm-go/extensions/frontend-gray/README.md b/plugins/wasm-go/extensions/frontend-gray/README.md index c54293062..8d21869cf 100644 --- a/plugins/wasm-go/extensions/frontend-gray/README.md +++ b/plugins/wasm-go/extensions/frontend-gray/README.md @@ -20,6 +20,7 @@ description: 前端灰度插件配置参考 | `localStorageGrayKey` | string | 非必填 | - | 使用JWT鉴权方式,用户ID的唯一标识来自`localStorage`中,如果配置了当前参数,则`grayKey`失效 | | `graySubKey` | string | 非必填 | - | 用户身份信息可能以JSON形式透出,比如:`userInfo:{ userCode:"001" }`,当前例子`graySubKey`取值为`userCode` | | `userStickyMaxAge` | int | 非必填 | 172800 | 用户粘滞的时长:单位为秒,默认为`172800`,2天时间 | +| `includePathPrefixes` | array of strings | 非必填 | - | 强制处理的路径。例如,在 微前端 场景下,XHR 接口如: `/resource/xxx`本质是一个资源请求,需要走插件转发逻辑。 | | `skippedPathPrefixes` | array of strings | 非必填 | - | 用于排除特定路径,避免当前插件处理这些请求。例如,在 rewrite 场景下,XHR 接口请求 `/api/xxx` 如果经过插件转发逻辑,可能会导致非预期的结果。 | | `skippedByHeaders` | map of string to string | 非必填 | - | 用于通过请求头过滤,指定哪些请求不被当前插件 处理。`skippedPathPrefixes` 的优先级高于当前配置,且页面HTML请求不受本配置的影响。若本配置为空,默认会判断`sec-fetch-mode=cors`以及`upgrade=websocket`两个header头,进行过滤 | diff --git a/plugins/wasm-go/extensions/frontend-gray/config/config.go b/plugins/wasm-go/extensions/frontend-gray/config/config.go index 4a56821d2..b624efa3b 100644 --- a/plugins/wasm-go/extensions/frontend-gray/config/config.go +++ b/plugins/wasm-go/extensions/frontend-gray/config/config.go @@ -64,6 +64,7 @@ type GrayConfig struct { BackendGrayTag string Injection *Injection SkippedPathPrefixes []string + IncludePathPrefixes []string SkippedByHeaders map[string]string } @@ -97,6 +98,7 @@ func JsonToGrayConfig(json gjson.Result, grayConfig *GrayConfig) { grayConfig.Html = json.Get("html").String() grayConfig.SkippedPathPrefixes = convertToStringList(json.Get("skippedPathPrefixes").Array()) grayConfig.SkippedByHeaders = convertToStringMap(json.Get("skippedByHeaders")) + grayConfig.IncludePathPrefixes = convertToStringList(json.Get("includePathPrefixes").Array()) if grayConfig.UserStickyMaxAge == "" { // 默认值2天 diff --git a/plugins/wasm-go/extensions/frontend-gray/util/utils.go b/plugins/wasm-go/extensions/frontend-gray/util/utils.go index a1a62a8fd..557bf3746 100644 --- a/plugins/wasm-go/extensions/frontend-gray/util/utils.go +++ b/plugins/wasm-go/extensions/frontend-gray/util/utils.go @@ -64,7 +64,13 @@ func IsRequestSkippedByHeaders(grayConfig config.GrayConfig) bool { } func IsGrayEnabled(grayConfig config.GrayConfig, requestPath string) bool { - // 当前路径中前缀为 SkipedRoute,则不走插件逻辑 + for _, prefix := range grayConfig.IncludePathPrefixes { + if strings.HasPrefix(requestPath, prefix) { + return true + } + } + + // 当前路径中前缀为 SkippedPathPrefixes,则不走插件逻辑 for _, prefix := range grayConfig.SkippedPathPrefixes { if strings.HasPrefix(requestPath, prefix) { return false