Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
e9fe6fb4
提交
e9fe6fb4
authored
5月 28, 2025
作者:
mooncake9527
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
err notice
上级
abd8b6e1
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
52 行增加
和
27 行删除
+52
-27
logging.go
gin/middleware/logging.go
+18
-18
logging_test.go
gin/middleware/logging_test.go
+4
-4
requstid.go
gin/middleware/requstid.go
+9
-0
response.go
gin/response/response.go
+4
-0
ctx.go
gin/xctx/ctx.go
+13
-0
xslice.go
utils/xslice/xslice.go
+4
-5
没有找到文件。
gin/middleware/logging.go
浏览文件 @
e9fe6fb4
...
...
@@ -36,18 +36,18 @@ type Option func(*options)
func
defaultOptions
()
*
options
{
return
&
options
{
maxLength
:
defaultMaxLength
,
log
:
defaultLogger
,
ignoreRoutes
:
defaultIgnoreRoutes
,
requestIDFrom
:
0
,
maxLength
:
defaultMaxLength
,
log
:
defaultLogger
,
ignoreRoutes
:
defaultIgnoreRoutes
,
traceIDFrom
:
0
,
}
}
type
options
struct
{
maxLength
int
log
*
zap
.
Logger
ignoreRoutes
map
[
string
]
struct
{}
requestIDFrom
int
// 0: ignore, 1: from context, 2: from header
maxLength
int
log
*
zap
.
Logger
ignoreRoutes
map
[
string
]
struct
{}
traceIDFrom
int
// 0: ignore, 1: from context, 2: from header
}
func
(
o
*
options
)
apply
(
opts
...
Option
)
{
...
...
@@ -84,17 +84,17 @@ func WithIgnoreRoutes(routes ...string) Option {
}
}
// With
Request
IDFromContext name is field in context, default value is request_id
func
With
Request
IDFromContext
()
Option
{
// With
Trace
IDFromContext name is field in context, default value is request_id
func
With
Trace
IDFromContext
()
Option
{
return
func
(
o
*
options
)
{
o
.
request
IDFrom
=
1
o
.
trace
IDFrom
=
1
}
}
// With
Request
IDFromHeader name is field in header, default value is X-Request-Id
func
With
Request
IDFromHeader
()
Option
{
// With
Trace
IDFromHeader name is field in header, default value is X-Request-Id
func
With
Trace
IDFromHeader
()
Option
{
return
func
(
o
*
options
)
{
o
.
request
IDFrom
=
2
o
.
trace
IDFrom
=
2
}
}
...
...
@@ -174,14 +174,14 @@ func Logging(opts ...Option) gin.HandlerFunc {
}
reqID
:=
""
if
o
.
request
IDFrom
==
1
{
if
o
.
trace
IDFrom
==
1
{
if
v
,
isExist
:=
c
.
Get
(
ctxUtil
.
ContextTraceIDKey
);
isExist
{
if
requestID
,
ok
:=
v
.
(
string
);
ok
{
reqID
=
requestID
fields
=
append
(
fields
,
zap
.
String
(
ctxUtil
.
ContextTraceIDKey
,
reqID
))
}
}
}
else
if
o
.
request
IDFrom
==
2
{
}
else
if
o
.
trace
IDFrom
==
2
{
reqID
=
c
.
Request
.
Header
.
Get
(
ctxUtil
.
HeaderXRequestIDKey
)
fields
=
append
(
fields
,
zap
.
String
(
ctxUtil
.
ContextTraceIDKey
,
reqID
))
}
...
...
@@ -232,13 +232,13 @@ func SimpleLog(opts ...Option) gin.HandlerFunc {
}
reqID
:=
""
if
o
.
request
IDFrom
==
1
{
if
o
.
trace
IDFrom
==
1
{
if
v
,
isExist
:=
c
.
Get
(
ctxUtil
.
ContextTraceIDKey
);
isExist
{
if
requestID
,
ok
:=
v
.
(
string
);
ok
{
reqID
=
requestID
}
}
}
else
if
o
.
request
IDFrom
==
2
{
}
else
if
o
.
trace
IDFrom
==
2
{
reqID
=
c
.
Request
.
Header
.
Get
(
ctxUtil
.
HeaderXRequestIDKey
)
}
...
...
gin/middleware/logging_test.go
浏览文件 @
e9fe6fb4
...
...
@@ -32,8 +32,8 @@ func runLogHTTPServer() string {
r
.
Use
(
Logging
(
WithLog
(
logger
.
Get
()),
WithMaxLen
(
40
),
With
Request
IDFromHeader
(),
With
Request
IDFromContext
(),
With
Trace
IDFromHeader
(),
With
Trace
IDFromContext
(),
WithIgnoreRoutes
(
"/ping"
),
// ignore path /ping
))
...
...
@@ -162,8 +162,8 @@ func runLogHTTPServer2() string {
r
.
Use
(
SimpleLog
(
WithLog
(
logger
.
Get
()),
WithMaxLen
(
200
),
With
Request
IDFromContext
(),
With
Request
IDFromHeader
(),
With
Trace
IDFromContext
(),
With
Trace
IDFromHeader
(),
))
pingFun
:=
func
(
c
*
gin
.
Context
)
{
...
...
gin/middleware/requstid.go
浏览文件 @
e9fe6fb4
...
...
@@ -123,6 +123,15 @@ func WrapCtx(c *gin.Context) context.Context {
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
func
AdaptCtx
(
ctx
context
.
Context
)
(
*
gin
.
Context
,
context
.
Context
)
{
c
,
ok
:=
ctx
.
(
*
gin
.
Context
)
...
...
gin/response/response.go
浏览文件 @
e9fe6fb4
...
...
@@ -59,6 +59,10 @@ func writeContentType(w http.ResponseWriter, value []string) {
func
writeJSON
(
c
*
gin
.
Context
,
code
int
,
res
interface
{})
{
c
.
Writer
.
WriteHeader
(
code
)
writeContentType
(
c
.
Writer
,
jsonContentType
)
//if res != nil {
// resBytes, _ := json.Marshal(res)
// c.Set(xctx.KeyResp, resBytes)
//}
err
:=
json
.
NewEncoder
(
c
.
Writer
)
.
Encode
(
res
)
if
err
!=
nil
{
fmt
.
Printf
(
"json encode error, err = %s
\n
"
,
err
.
Error
())
...
...
gin/xctx/ctx.go
浏览文件 @
e9fe6fb4
...
...
@@ -32,6 +32,8 @@ var (
KeyUName
=
"uname"
KeyToken
=
"token"
KeyUser
=
"user"
KeyResp
=
"resp"
)
var
(
...
...
@@ -202,3 +204,14 @@ func getCtxVal[T any](c context.Context, key string) T {
var
t
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
}
utils/xslice/xslice.go
浏览文件 @
e9fe6fb4
...
...
@@ -2,6 +2,7 @@ package xslice
import
(
"fmt"
"gitlab.wanzhuangkj.com/tush/xpkg/errcode"
"gitlab.wanzhuangkj.com/tush/xpkg/xtype"
"strings"
...
...
@@ -230,19 +231,17 @@ func GetIDMapSliceIDs[ID xtype.Key, T IID[ID]](m map[ID][]*T) []ID {
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
{
return
nil
}
if
len
(
bizSlice
)
<
len
(
ids
)
{
if
len
(
bizSlice
)
==
0
{
var
t
T
return
xerror
.
NewC
(
errCode
,
fmt
.
Sprintf
(
"未查询到%s"
,
t
.
TableName
()))
return
xerror
.
NewC
(
errCode
.
Code
(),
errCode
.
Msg
())
}
deltaIDs
:=
SetDifference
(
ids
,
GetIDs
[
ID
](
bizSlice
))
if
len
(
deltaIDs
)
>
0
{
var
t
T
return
xerror
.
NewC
(
errCode
,
fmt
.
Sprintf
(
"未查询到%s[ids:%s]"
,
t
.
TableName
(),
Join
(
deltaIDs
,
","
)))
return
xerror
.
NewC
(
errCode
.
Code
(),
fmt
.
Sprintf
(
"%s[ids:%s]"
,
errCode
.
Msg
(),
Join
(
deltaIDs
,
","
)))
}
}
return
nil
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论