feat: 添加代理请求的用户信息传递
This commit is contained in:
17
main.go
17
main.go
@@ -12,8 +12,10 @@ import (
|
|||||||
"gitea.com/red-future/common/http"
|
"gitea.com/red-future/common/http"
|
||||||
"gitea.com/red-future/common/middleware"
|
"gitea.com/red-future/common/middleware"
|
||||||
_ "gitea.com/red-future/common/swagger"
|
_ "gitea.com/red-future/common/swagger"
|
||||||
|
"gitea.com/red-future/common/utils"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/net/ghttp"
|
"github.com/gogf/gf/v2/net/ghttp"
|
||||||
|
"github.com/gogf/gf/v2/util/gconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func StartServerProxy() {
|
func StartServerProxy() {
|
||||||
@@ -64,6 +66,21 @@ func StartServerProxy() {
|
|||||||
proxy.ErrorHandler = func(writer netHttp.ResponseWriter, request *netHttp.Request, e error) {
|
proxy.ErrorHandler = func(writer netHttp.ResponseWriter, request *netHttp.Request, e error) {
|
||||||
writer.WriteHeader(netHttp.StatusBadGateway)
|
writer.WriteHeader(netHttp.StatusBadGateway)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
user, err := utils.GetUserInfo(r.GetCtx())
|
||||||
|
if err != nil {
|
||||||
|
g.Log().Errorf(r.GetCtx(), "获取用户信息失败: %v", err)
|
||||||
|
r.Response.Status = 500
|
||||||
|
r.Response.WriteJsonExit(map[string]interface{}{
|
||||||
|
"success": false,
|
||||||
|
"code": 500,
|
||||||
|
"message": "获取用户信息失败",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 将用户信息通过 Header 传递给下游服务
|
||||||
|
r.Request.Header.Set("X-User-Info", gconv.String(&user))
|
||||||
|
|
||||||
proxy.ServeHTTP(r.Response.Writer, r.Request)
|
proxy.ServeHTTP(r.Response.Writer, r.Request)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user