mirror of
https://github.com/alibaba/higress.git
synced 2026-04-21 20:17:29 +08:00
feat: support frontend-gray plugin's envoy.yaml file to host HTML (#1343)
Co-authored-by: Kent Dong <ch3cho@qq.com>
This commit is contained in:
@@ -184,12 +184,33 @@ func onHttpResponseBody(ctx wrapper.HttpContext, grayConfig config.GrayConfig, b
|
||||
isPageRequest = false // 默认值
|
||||
}
|
||||
frontendVersion := ctx.GetContext(config.XPreHigressTag).(string)
|
||||
|
||||
isNotFound, ok := ctx.GetContext(config.IsNotFound).(bool)
|
||||
if !ok {
|
||||
isNotFound = false // 默认值
|
||||
}
|
||||
|
||||
// 检查是否存在自定义 HTML, 如有则省略 rewrite.indexRouting 的内容
|
||||
if grayConfig.Html != "" {
|
||||
log.Debugf("Returning custom HTML from config.")
|
||||
// 替换响应体为 config.Html 内容
|
||||
if err := proxywasm.ReplaceHttpResponseBody([]byte(grayConfig.Html)); err != nil {
|
||||
log.Errorf("Error replacing response body: %v", err)
|
||||
return types.ActionContinue
|
||||
}
|
||||
|
||||
newHtml := util.InjectContent(grayConfig.Html, grayConfig.Injection)
|
||||
// 替换当前html加载的动态文件版本
|
||||
newHtml = strings.ReplaceAll(newHtml, "{version}", frontendVersion)
|
||||
|
||||
// 最终替换响应体
|
||||
if err := proxywasm.ReplaceHttpResponseBody([]byte(newHtml)); err != nil {
|
||||
log.Errorf("Error replacing injected response body: %v", err)
|
||||
return types.ActionContinue
|
||||
}
|
||||
|
||||
return types.ActionContinue
|
||||
}
|
||||
|
||||
if isPageRequest && isNotFound && grayConfig.Rewrite.Host != "" && grayConfig.Rewrite.NotFound != "" {
|
||||
client := wrapper.NewClusterClient(wrapper.RouteCluster{Host: grayConfig.Rewrite.Host})
|
||||
|
||||
@@ -204,30 +225,13 @@ func onHttpResponseBody(ctx wrapper.HttpContext, grayConfig config.GrayConfig, b
|
||||
// 将原始字节转换为字符串
|
||||
newBody := string(body)
|
||||
|
||||
// 收集需要插入的内容
|
||||
headInjection := strings.Join(grayConfig.Injection.Head, "\n")
|
||||
bodyFirstInjection := strings.Join(grayConfig.Injection.Body.First, "\n")
|
||||
bodyLastInjection := strings.Join(grayConfig.Injection.Body.Last, "\n")
|
||||
|
||||
// 使用 strings.Builder 来提高性能
|
||||
var sb strings.Builder
|
||||
// 预分配内存,避免多次内存分配
|
||||
sb.Grow(len(newBody) + len(headInjection) + len(bodyFirstInjection) + len(bodyLastInjection))
|
||||
sb.WriteString(newBody)
|
||||
|
||||
// 进行替换
|
||||
content := sb.String()
|
||||
content = strings.ReplaceAll(content, "</head>", fmt.Sprintf("%s\n</head>", headInjection))
|
||||
content = strings.ReplaceAll(content, "<body>", fmt.Sprintf("<body>\n%s", bodyFirstInjection))
|
||||
content = strings.ReplaceAll(content, "</body>", fmt.Sprintf("%s\n</body>", bodyLastInjection))
|
||||
|
||||
// 最终结果
|
||||
newBody = content
|
||||
newBody = util.InjectContent(newBody, grayConfig.Injection)
|
||||
|
||||
if err := proxywasm.ReplaceHttpResponseBody([]byte(newBody)); err != nil {
|
||||
return types.ActionContinue
|
||||
}
|
||||
}
|
||||
|
||||
return types.ActionContinue
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user