change layout
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
package dialer
|
||||
|
||||
import (
|
||||
"bff/internal/response"
|
||||
"context"
|
||||
"fmt"
|
||||
"lone-services/bff/internal/response"
|
||||
"lone-services/pkg/discovery"
|
||||
"net"
|
||||
|
||||
"pkg.local/discovery"
|
||||
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
|
||||
@@ -5,8 +5,7 @@ import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"encoding/base64"
|
||||
|
||||
"pkg.local/utils"
|
||||
"lone-services/pkg/utils"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package response
|
||||
|
||||
import "encoding/json"
|
||||
import jsoniter "github.com/json-iterator/go"
|
||||
|
||||
var _ Error = (*err)(nil)
|
||||
|
||||
@@ -53,7 +53,7 @@ func (e *err) ToString() string {
|
||||
Data: e.Data,
|
||||
}
|
||||
|
||||
raw, _ := json.Marshal(err)
|
||||
raw, _ := jsoniter.Marshal(err)
|
||||
return string(raw)
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@ package response
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
@@ -43,7 +43,7 @@ func Wrap(next http.HandlerFunc) http.HandlerFunc {
|
||||
raw := bytes.TrimSpace(bw.buf.Bytes())
|
||||
var data responseData
|
||||
if len(raw) > 0 {
|
||||
if err := json.Unmarshal(raw, &data); err != nil {
|
||||
if err := jsoniter.Unmarshal(raw, &data); err != nil {
|
||||
ctrl.Fail(w)
|
||||
return
|
||||
}
|
||||
@@ -63,7 +63,7 @@ func unwrapJSONData(v any) any {
|
||||
return nil
|
||||
}
|
||||
var parsed any
|
||||
if err := json.Unmarshal([]byte(s), &parsed); err != nil {
|
||||
if err := jsoniter.Unmarshal([]byte(s), &parsed); err != nil {
|
||||
return v
|
||||
}
|
||||
return parsed
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"lone-services/pkg/utils"
|
||||
"net/http"
|
||||
|
||||
"pkg.local/utils"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
type BaseController struct{}
|
||||
@@ -27,12 +27,12 @@ func (b *BaseController) Out(w http.ResponseWriter, data any, cm Error) {
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_ = json.NewEncoder(w).Encode(out)
|
||||
_ = jsoniter.NewEncoder(w).Encode(out)
|
||||
}
|
||||
|
||||
func (b *BaseController) OutPut(w http.ResponseWriter, code int32, data any, msg string) {
|
||||
if code == utils.Ok.Code && !utils.GetConfigBool("encrypt.debug") {
|
||||
jsonData, err := json.Marshal(data)
|
||||
jsonData, err := jsoniter.Marshal(data)
|
||||
if err != nil {
|
||||
fmt.Printf("response encrypt: json.Marshal failed: %v\n", err)
|
||||
msg = utils.ErrorEncryptAesError.Msg
|
||||
@@ -56,5 +56,5 @@ func (b *BaseController) OutPut(w http.ResponseWriter, code int32, data any, msg
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_ = json.NewEncoder(w).Encode(out)
|
||||
_ = jsoniter.NewEncoder(w).Encode(out)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package response
|
||||
|
||||
import (
|
||||
"context"
|
||||
"lone-services/pkg/utils"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
@@ -13,29 +14,36 @@ import (
|
||||
type ctxKey string
|
||||
|
||||
const (
|
||||
CtxUserId ctxKey = "X-User-Id"
|
||||
CtxUserName ctxKey = "X-User-Name"
|
||||
CtxRefresh ctxKey = "X-Refresh"
|
||||
CtxServiceCode ctxKey = "X-Service-Code"
|
||||
CtxUserId ctxKey = "X-User-Id"
|
||||
CtxUserName ctxKey = "X-User-Name"
|
||||
CtxRefresh ctxKey = "X-Refresh"
|
||||
CtxClientIP ctxKey = "X-Client-Ip"
|
||||
CtxUserAgent ctxKey = "X-User-Agent"
|
||||
)
|
||||
|
||||
type UserInfo struct {
|
||||
ID int64
|
||||
Name string
|
||||
RawUID string
|
||||
Refresh string
|
||||
Valid bool
|
||||
ID int64
|
||||
Name string
|
||||
RawUID string
|
||||
Refresh string
|
||||
ClientIP string
|
||||
UserAgent string
|
||||
Valid bool
|
||||
}
|
||||
|
||||
func GetUserInfo(ctx context.Context) UserInfo {
|
||||
rawUID, _ := ctx.Value(CtxUserId).(string)
|
||||
name, _ := ctx.Value(CtxUserName).(string)
|
||||
refresh, _ := ctx.Value(CtxRefresh).(string)
|
||||
clientIP, _ := ctx.Value(CtxClientIP).(string)
|
||||
userAgent, _ := ctx.Value(CtxUserAgent).(string)
|
||||
|
||||
info := UserInfo{
|
||||
RawUID: rawUID,
|
||||
Name: name,
|
||||
Refresh: refresh,
|
||||
RawUID: rawUID,
|
||||
Name: name,
|
||||
Refresh: refresh,
|
||||
ClientIP: clientIP,
|
||||
UserAgent: userAgent,
|
||||
}
|
||||
if rawUID == "" {
|
||||
return info
|
||||
@@ -50,12 +58,27 @@ func GetUserInfo(ctx context.Context) UserInfo {
|
||||
return info
|
||||
}
|
||||
|
||||
// getRealClientIP 获取真实客户端IP,优先 X‑Forwarded‑For,其次 X‑Real‑IP,最后 RemoteAddr
|
||||
func getRealClientIP(r *http.Request) string {
|
||||
xff := r.Header.Get("X-Forwarded-For")
|
||||
if xff != "" {
|
||||
return xff
|
||||
}
|
||||
xri := r.Header.Get("X-Real-Ip")
|
||||
if xri != "" {
|
||||
return xri
|
||||
}
|
||||
return r.RemoteAddr
|
||||
}
|
||||
|
||||
func UserReadMiddleware(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
newCtx := r.Context()
|
||||
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"))
|
||||
newCtx = context.WithValue(newCtx, CtxClientIP, getRealClientIP(r))
|
||||
newCtx = context.WithValue(newCtx, CtxUserAgent, r.UserAgent())
|
||||
next(w, r.WithContext(newCtx))
|
||||
}
|
||||
}
|
||||
@@ -69,9 +92,15 @@ func UserClientInterceptor(ctx context.Context, method string, req, reply any, c
|
||||
md.Set("x-user-name", url.QueryEscape(user.Name))
|
||||
}
|
||||
|
||||
if user.Refresh != "" {
|
||||
if user.Refresh != utils.StringEmpty {
|
||||
md.Set("x-refresh", user.Refresh)
|
||||
}
|
||||
if user.ClientIP != utils.StringEmpty {
|
||||
md.Set("x-client-ip", user.ClientIP)
|
||||
}
|
||||
if user.UserAgent != utils.StringEmpty {
|
||||
md.Set("x-user-agent", url.QueryEscape(user.UserAgent))
|
||||
}
|
||||
|
||||
ctx = metadata.NewOutgoingContext(ctx, md)
|
||||
return invoker(ctx, method, req, reply, cc, opts...)
|
||||
|
||||
Reference in New Issue
Block a user