提交 e9fe6fb4 authored 作者: mooncake9527's avatar mooncake9527

err notice

上级 abd8b6e1
...@@ -36,18 +36,18 @@ type Option func(*options) ...@@ -36,18 +36,18 @@ type Option func(*options)
func defaultOptions() *options { func defaultOptions() *options {
return &options{ return &options{
maxLength: defaultMaxLength, maxLength: defaultMaxLength,
log: defaultLogger, log: defaultLogger,
ignoreRoutes: defaultIgnoreRoutes, ignoreRoutes: defaultIgnoreRoutes,
requestIDFrom: 0, traceIDFrom: 0,
} }
} }
type options struct { type options struct {
maxLength int maxLength int
log *zap.Logger log *zap.Logger
ignoreRoutes map[string]struct{} ignoreRoutes map[string]struct{}
requestIDFrom int // 0: ignore, 1: from context, 2: from header traceIDFrom int // 0: ignore, 1: from context, 2: from header
} }
func (o *options) apply(opts ...Option) { func (o *options) apply(opts ...Option) {
...@@ -84,17 +84,17 @@ func WithIgnoreRoutes(routes ...string) Option { ...@@ -84,17 +84,17 @@ func WithIgnoreRoutes(routes ...string) Option {
} }
} }
// WithRequestIDFromContext name is field in context, default value is request_id // WithTraceIDFromContext name is field in context, default value is request_id
func WithRequestIDFromContext() Option { func WithTraceIDFromContext() Option {
return func(o *options) { return func(o *options) {
o.requestIDFrom = 1 o.traceIDFrom = 1
} }
} }
// WithRequestIDFromHeader name is field in header, default value is X-Request-Id // WithTraceIDFromHeader name is field in header, default value is X-Request-Id
func WithRequestIDFromHeader() Option { func WithTraceIDFromHeader() Option {
return func(o *options) { return func(o *options) {
o.requestIDFrom = 2 o.traceIDFrom = 2
} }
} }
...@@ -174,14 +174,14 @@ func Logging(opts ...Option) gin.HandlerFunc { ...@@ -174,14 +174,14 @@ func Logging(opts ...Option) gin.HandlerFunc {
} }
reqID := "" reqID := ""
if o.requestIDFrom == 1 { if o.traceIDFrom == 1 {
if v, isExist := c.Get(ctxUtil.ContextTraceIDKey); isExist { if v, isExist := c.Get(ctxUtil.ContextTraceIDKey); isExist {
if requestID, ok := v.(string); ok { if requestID, ok := v.(string); ok {
reqID = requestID reqID = requestID
fields = append(fields, zap.String(ctxUtil.ContextTraceIDKey, reqID)) fields = append(fields, zap.String(ctxUtil.ContextTraceIDKey, reqID))
} }
} }
} else if o.requestIDFrom == 2 { } else if o.traceIDFrom == 2 {
reqID = c.Request.Header.Get(ctxUtil.HeaderXRequestIDKey) reqID = c.Request.Header.Get(ctxUtil.HeaderXRequestIDKey)
fields = append(fields, zap.String(ctxUtil.ContextTraceIDKey, reqID)) fields = append(fields, zap.String(ctxUtil.ContextTraceIDKey, reqID))
} }
...@@ -232,13 +232,13 @@ func SimpleLog(opts ...Option) gin.HandlerFunc { ...@@ -232,13 +232,13 @@ func SimpleLog(opts ...Option) gin.HandlerFunc {
} }
reqID := "" reqID := ""
if o.requestIDFrom == 1 { if o.traceIDFrom == 1 {
if v, isExist := c.Get(ctxUtil.ContextTraceIDKey); isExist { if v, isExist := c.Get(ctxUtil.ContextTraceIDKey); isExist {
if requestID, ok := v.(string); ok { if requestID, ok := v.(string); ok {
reqID = requestID reqID = requestID
} }
} }
} else if o.requestIDFrom == 2 { } else if o.traceIDFrom == 2 {
reqID = c.Request.Header.Get(ctxUtil.HeaderXRequestIDKey) reqID = c.Request.Header.Get(ctxUtil.HeaderXRequestIDKey)
} }
......
...@@ -32,8 +32,8 @@ func runLogHTTPServer() string { ...@@ -32,8 +32,8 @@ func runLogHTTPServer() string {
r.Use(Logging( r.Use(Logging(
WithLog(logger.Get()), WithLog(logger.Get()),
WithMaxLen(40), WithMaxLen(40),
WithRequestIDFromHeader(), WithTraceIDFromHeader(),
WithRequestIDFromContext(), WithTraceIDFromContext(),
WithIgnoreRoutes("/ping"), // ignore path /ping WithIgnoreRoutes("/ping"), // ignore path /ping
)) ))
...@@ -162,8 +162,8 @@ func runLogHTTPServer2() string { ...@@ -162,8 +162,8 @@ func runLogHTTPServer2() string {
r.Use(SimpleLog( r.Use(SimpleLog(
WithLog(logger.Get()), WithLog(logger.Get()),
WithMaxLen(200), WithMaxLen(200),
WithRequestIDFromContext(), WithTraceIDFromContext(),
WithRequestIDFromHeader(), WithTraceIDFromHeader(),
)) ))
pingFun := func(c *gin.Context) { pingFun := func(c *gin.Context) {
......
...@@ -123,6 +123,15 @@ func WrapCtx(c *gin.Context) context.Context { ...@@ -123,6 +123,15 @@ func WrapCtx(c *gin.Context) context.Context {
return ctx return ctx
} }
func WrapAsyncCtx(c *gin.Context) context.Context {
ctx := context.WithValue(context.Background(), ctxUtil.ContextTraceIDKey, c.GetString(ctxUtil.ContextTraceIDKey)) //nolint
for k, v := range c.Keys {
ctx = context.WithValue(ctx, k, v) //nolint
}
ctx = context.WithValue(ctx, ctxUtil.GinContextKey, c) //nolint
return ctx
}
// AdaptCtx adapt context, if ctx is gin.Context, return gin.Context and context of the transformation // AdaptCtx adapt context, if ctx is gin.Context, return gin.Context and context of the transformation
func AdaptCtx(ctx context.Context) (*gin.Context, context.Context) { func AdaptCtx(ctx context.Context) (*gin.Context, context.Context) {
c, ok := ctx.(*gin.Context) c, ok := ctx.(*gin.Context)
......
...@@ -59,6 +59,10 @@ func writeContentType(w http.ResponseWriter, value []string) { ...@@ -59,6 +59,10 @@ func writeContentType(w http.ResponseWriter, value []string) {
func writeJSON(c *gin.Context, code int, res interface{}) { func writeJSON(c *gin.Context, code int, res interface{}) {
c.Writer.WriteHeader(code) c.Writer.WriteHeader(code)
writeContentType(c.Writer, jsonContentType) writeContentType(c.Writer, jsonContentType)
//if res != nil {
// resBytes, _ := json.Marshal(res)
// c.Set(xctx.KeyResp, resBytes)
//}
err := json.NewEncoder(c.Writer).Encode(res) err := json.NewEncoder(c.Writer).Encode(res)
if err != nil { if err != nil {
fmt.Printf("json encode error, err = %s\n", err.Error()) fmt.Printf("json encode error, err = %s\n", err.Error())
......
...@@ -32,6 +32,8 @@ var ( ...@@ -32,6 +32,8 @@ var (
KeyUName = "uname" KeyUName = "uname"
KeyToken = "token" KeyToken = "token"
KeyUser = "user" KeyUser = "user"
KeyResp = "resp"
) )
var ( var (
...@@ -202,3 +204,14 @@ func getCtxVal[T any](c context.Context, key string) T { ...@@ -202,3 +204,14 @@ func getCtxVal[T any](c context.Context, key string) T {
var t T var t T
return t return t
} }
func GetResp(c *gin.Context) []byte {
v, ok1 := c.Get(KeyResp)
if ok1 {
ret, ok2 := v.([]byte)
if ok2 {
return ret
}
}
return nil
}
...@@ -2,6 +2,7 @@ package xslice ...@@ -2,6 +2,7 @@ package xslice
import ( import (
"fmt" "fmt"
"gitlab.wanzhuangkj.com/tush/xpkg/errcode"
"gitlab.wanzhuangkj.com/tush/xpkg/xtype" "gitlab.wanzhuangkj.com/tush/xpkg/xtype"
"strings" "strings"
...@@ -230,19 +231,17 @@ func GetIDMapSliceIDs[ID xtype.Key, T IID[ID]](m map[ID][]*T) []ID { ...@@ -230,19 +231,17 @@ func GetIDMapSliceIDs[ID xtype.Key, T IID[ID]](m map[ID][]*T) []ID {
return st.ToList() return st.ToList()
} }
func CompareSlice[ID xtype.Key, T IIDTable[ID], BizSlice ~[]*T](ids []ID, bizSlice BizSlice, errCode int) error { func CompareSlice[ID xtype.Key, T IIDTable[ID], BizSlice ~[]*T](ids []ID, bizSlice BizSlice, errCode *errcode.Error) error {
if len(ids) == 0 { if len(ids) == 0 {
return nil return nil
} }
if len(bizSlice) < len(ids) { if len(bizSlice) < len(ids) {
if len(bizSlice) == 0 { if len(bizSlice) == 0 {
var t T return xerror.NewC(errCode.Code(), errCode.Msg())
return xerror.NewC(errCode, fmt.Sprintf("未查询到%s", t.TableName()))
} }
deltaIDs := SetDifference(ids, GetIDs[ID](bizSlice)) deltaIDs := SetDifference(ids, GetIDs[ID](bizSlice))
if len(deltaIDs) > 0 { if len(deltaIDs) > 0 {
var t T return xerror.NewC(errCode.Code(), fmt.Sprintf("%s[ids:%s]", errCode.Msg(), Join(deltaIDs, ",")))
return xerror.NewC(errCode, fmt.Sprintf("未查询到%s[ids:%s]", t.TableName(), Join(deltaIDs, ",")))
} }
} }
return nil return nil
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论