2 Star 9 Fork 1

os-lee/easy-paas

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
url_rewrite.go 983 Bytes
一键复制 编辑 原始数据 按行查看 历史
lee 提交于 2024-07-11 15:01 . websocket路由支持url重写,负载均衡
package middleware_http
import (
"gitee.com/os-lee/easy-paas/common"
"gitee.com/os-lee/easy-paas/common/e"
"gitee.com/os-lee/easy-paas/gateway/internal/dao"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"regexp"
"strings"
)
// UrlRewrite url重写
func UrlRewrite() gin.HandlerFunc {
return func(c *gin.Context) {
serverInterface, ok := c.Get("service")
if !ok {
common.ResponseError(c, e.ErrGateway, errors.New("从上下文中获取服务失败"))
c.Abort()
return
}
serviceDetail := serverInterface.(*dao.ServiceDetail)
for _, item := range strings.Split(serviceDetail.HTTPRule.UrlRewrite, ",") {
items := strings.Split(item, " ")
if len(items) != 2 {
continue
}
regexp, err := regexp.Compile(items[0])
if err != nil {
//fmt.Println("regexp.Compile err",err)
continue
}
replacePath := regexp.ReplaceAll([]byte(c.Request.URL.Path), []byte(items[1]))
c.Request.URL.Path = string(replacePath)
}
c.Next()
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/os-lee/easy-paas.git
git@gitee.com:os-lee/easy-paas.git
os-lee
easy-paas
easy-paas
b4985bcf7af8

搜索帮助