This commit is contained in:
2026-09-04 17:51:30 +08:00
parent 04402a8bdf
commit e615be6ad7
18 changed files with 449 additions and 124 deletions
+2 -2
View File
@@ -3,7 +3,7 @@ package dialer
import (
"context"
"fmt"
"lone-services/bff/internal/response"
"lone-services/bff/internal/request"
"lone-services/pkg/discovery"
"net"
"time"
@@ -25,7 +25,7 @@ func Nacos(conf zrpc.RpcClientConf) zrpc.Client {
return zrpc.MustNewClient(cliConf,
zrpc.WithDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
zrpc.WithDialOption(grpc.WithUnaryInterceptor(response.UserClientInterceptor)),
zrpc.WithDialOption(grpc.WithUnaryInterceptor(request.UserClientInterceptor)),
zrpc.WithDialOption(grpc.WithDefaultCallOptions(grpc.WaitForReady(true))),
zrpc.WithDialOption(grpc.WithConnectParams(grpc.ConnectParams{
MinConnectTimeout: 2 * time.Second,
@@ -1,11 +1,14 @@
package response
package request
import (
"bytes"
"context"
"io"
"lone-services/pkg/utils"
"net/http"
"net/url"
"strconv"
"strings"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
@@ -14,23 +17,32 @@ import (
type ctxKey string
const (
CtxUserId ctxKey = "X-User-Id"
CtxUserName ctxKey = "X-User-Name"
CtxRefresh ctxKey = "X-Refresh"
CtxClientIP ctxKey = "X-Client-Ip"
CtxUserAgent ctxKey = "X-User-Agent"
CtxClientCode ctxKey = "X-Client-Code"
CtxSaleId ctxKey = "X-Sale-Id"
CtxSaleName ctxKey = "X-Sale-Name"
CtxSaleMobile ctxKey = "X-Sale-Mobile"
CtxSaleProvince ctxKey = "X-Sale-Province"
CtxStoreId ctxKey = "X-Store-Id"
CtxStoreName ctxKey = "X-Store-Name"
CtxGroupId ctxKey = "X-Group-Id"
CtxGroupName ctxKey = "X-Group-Name"
CtxUserType ctxKey = "X-User-Type"
CtxUserId ctxKey = "X-User-Id"
CtxUserName ctxKey = "X-User-Name"
CtxRefresh ctxKey = "X-Refresh"
CtxClientIP ctxKey = "X-Client-Ip"
CtxUserAgent ctxKey = "X-User-Agent"
CtxClientCode ctxKey = "X-Client-Code"
CtxSaleId ctxKey = "X-Sale-Id"
CtxSaleName ctxKey = "X-Sale-Name"
CtxSaleMobile ctxKey = "X-Sale-Mobile"
CtxSaleProvince ctxKey = "X-Sale-Province"
CtxStoreId ctxKey = "X-Store-Id"
CtxStoreName ctxKey = "X-Store-Name"
CtxGroupId ctxKey = "X-Group-Id"
CtxGroupName ctxKey = "X-Group-Name"
CtxUserType ctxKey = "X-User-Type"
CtxCallbackRawBody ctxKey = "callback_raw_body"
CtxCallbackHeaders ctxKey = "callback_headers"
CtxCallbackErr ctxKey = "callback_read_err"
)
var callbackPrefixes = []string{
"/api/v3/express/",
"/api/v3/other1/",
"/api/v3/other2/",
}
type UserInfo struct {
ID int64
Name string
@@ -113,6 +125,26 @@ func getRealClientIP(r *http.Request) string {
func UserReadMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
newCtx := r.Context()
if isCallbackPath(r.URL.Path) {
body, err := io.ReadAll(r.Body)
if err != nil {
newCtx = context.WithValue(newCtx, CtxCallbackRawBody, []byte(nil))
newCtx = context.WithValue(newCtx, CtxCallbackHeaders, http.Header(nil))
newCtx = context.WithValue(newCtx, CtxCallbackErr, err)
} else {
r.Body = io.NopCloser(bytes.NewBuffer(body))
header := make(http.Header)
for k, vv := range r.Header {
header[k] = vv
}
newCtx = context.WithValue(newCtx, CtxCallbackRawBody, body)
newCtx = context.WithValue(newCtx, CtxCallbackHeaders, header)
newCtx = context.WithValue(newCtx, CtxCallbackErr, error(nil))
}
next(w, r.WithContext(newCtx))
return
}
newCtx = context.WithValue(newCtx, CtxUserId, r.Header.Get("X-User-Id"))
newCtx = context.WithValue(newCtx, CtxUserName, r.Header.Get("X-User-Name"))
newCtx = context.WithValue(newCtx, CtxRefresh, r.Header.Get("X-Refresh"))
@@ -128,10 +160,20 @@ func UserReadMiddleware(next http.HandlerFunc) http.HandlerFunc {
newCtx = context.WithValue(newCtx, CtxStoreName, r.Header.Get("X-Store-Name"))
newCtx = context.WithValue(newCtx, CtxGroupId, r.Header.Get("X-Group-Id"))
newCtx = context.WithValue(newCtx, CtxGroupName, r.Header.Get("X-Group-Name"))
next(w, r.WithContext(newCtx))
}
}
func isCallbackPath(path string) bool {
for _, prefix := range callbackPrefixes {
if strings.HasPrefix(path, prefix) {
return true
}
}
return false
}
func setMDIfNotEmpty(md metadata.MD, key, value string) {
if value != utils.StringEmpty {
md.Set(key, value)