Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
3ce36762
提交
3ce36762
authored
6月 24, 2025
作者:
mooncake9527
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
cp req rsp
上级
96974b25
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
85 行增加
和
12 行删除
+85
-12
entity.go
gin/entity/entity.go
+13
-0
logging.go
gin/middleware/logging.go
+38
-10
response.go
gin/response/response.go
+8
-2
jsonUtils.go
utils/jsonUtils/jsonUtils.go
+26
-0
没有找到文件。
gin/entity/entity.go
0 → 100644
浏览文件 @
3ce36762
package
entity
type
CopyHttpReq
struct
{
Method
string
`json:"method"`
URL
string
`json:"url"`
Headers
map
[
string
][]
string
`json:"headers"`
Body
string
`json:"body"`
}
type
CopyHttpRsp
struct
{
Headers
map
[
string
][]
string
`json:"headers"`
Body
string
`json:"body"`
}
gin/middleware/logging.go
浏览文件 @
3ce36762
...
...
@@ -2,8 +2,7 @@ package middleware
import
(
"bytes"
"encoding/json"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/uriUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/entity"
"io"
"net/http"
"time"
...
...
@@ -185,14 +184,20 @@ func Logging(opts ...Option) gin.HandlerFunc {
)
}
c
.
Set
(
ctxUtil
.
KeyReqBody
,
buf
.
Bytes
())
if
c
.
Request
.
Method
==
http
.
MethodGet
&&
buf
.
Len
()
==
0
{
paramMap
,
err
:=
uriUtils
.
URLValuePairsToMap
(
c
.
Request
.
URL
.
RawQuery
)
if
err
==
nil
{
val
,
_
:=
json
.
Marshal
(
paramMap
)
c
.
Set
(
ctxUtil
.
KeyReqBody
,
val
)
}
}
copyHttpReq
:=
&
entity
.
CopyHttpReq
{}
copyHttpReq
.
Method
=
c
.
Request
.
Method
copyHttpReq
.
URL
=
c
.
Request
.
URL
.
String
()
copyHttpReq
.
Headers
=
c
.
Request
.
Header
copyHttpReq
.
Body
=
buf
.
String
()
c
.
Set
(
ctxUtil
.
KeyReqBody
,
copyHttpReq
)
//if c.Request.Method == http.MethodGet && buf.Len() == 0 {
// paramMap, err := uriUtils.URLValuePairsToMap(c.Request.URL.RawQuery)
// if err == nil {
// val, _ := json.Marshal(paramMap)
// c.Set(ctxUtil.KeyReqBody, val)
// }
//}
reqID
:=
""
if
o
.
traceIDFrom
==
1
{
...
...
@@ -236,10 +241,33 @@ func Logging(opts ...Option) gin.HandlerFunc {
if
reqID
!=
""
{
fields
=
append
(
fields
,
zap
.
String
(
ctxUtil
.
ContextTraceIDKey
,
reqID
))
}
//copyHttpRsp := CopyHttpRsp{}
//copyHttpRsp.Headers = c.Writer.Header()
//copyHttpRsp.Body = newWriter.body.Bytes()
//c.Set(ctxUtil.KeyRspBody, jsonUtils.ToJsonBytes(copyHttpRsp))
o
.
log
.
Info
(
">>>>>>>>>rsp"
,
fields
...
)
}
}
func
GetCopyReq
(
c
*
gin
.
Context
)
*
entity
.
CopyHttpReq
{
if
val
,
isExist
:=
c
.
Get
(
ctxUtil
.
KeyReqBody
);
isExist
{
if
req
,
ok
:=
val
.
(
*
entity
.
CopyHttpReq
);
ok
{
return
req
}
}
return
nil
}
func
GetCopyRsp
(
c
*
gin
.
Context
)
*
entity
.
CopyHttpRsp
{
if
val
,
isExist
:=
c
.
Get
(
ctxUtil
.
KeyRspBody
);
isExist
{
if
rsp
,
ok
:=
val
.
(
*
entity
.
CopyHttpRsp
);
ok
{
return
rsp
}
}
return
nil
}
// SimpleLog print response info
func
SimpleLog
(
opts
...
Option
)
gin
.
HandlerFunc
{
o
:=
defaultOptions
()
...
...
gin/response/response.go
浏览文件 @
3ce36762
...
...
@@ -3,6 +3,7 @@ package response
import
(
"encoding/json"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/entity"
"net/http"
"time"
...
...
@@ -59,8 +60,13 @@ func writeJSON(c *gin.Context, code int, res interface{}) {
writeContentType
(
c
.
Writer
,
jsonContentType
)
if
res
!=
nil
{
resBytes
,
_
:=
json
.
Marshal
(
res
)
c
.
Set
(
ctxUtils
.
KeyRspBody
,
resBytes
)
c
.
Writer
.
Write
(
resBytes
)
copyHttpRsp
:=
&
entity
.
CopyHttpRsp
{}
copyHttpRsp
.
Headers
=
c
.
Writer
.
Header
()
copyHttpRsp
.
Body
=
string
(
resBytes
)
c
.
Set
(
ctxUtils
.
KeyRspBody
,
copyHttpRsp
)
_
,
_
=
c
.
Writer
.
Write
(
resBytes
)
}
//err := json.NewEncoder(c.Writer).Encode(res)
//if err != nil {
...
...
utils/jsonUtils/jsonUtils.go
0 → 100644
浏览文件 @
3ce36762
package
jsonUtils
import
(
"encoding/json"
"errors"
)
func
ToJsonString
(
v
any
)
string
{
return
string
(
ToJsonBytes
(
v
))
}
func
ToJsonBytes
(
v
any
)
[]
byte
{
if
v
==
nil
{
return
[]
byte
{}
}
bytes
,
_
:=
json
.
Marshal
(
v
)
return
bytes
}
func
Unmarshal
(
data
[]
byte
,
v
any
)
error
{
err
:=
json
.
Unmarshal
(
data
,
v
)
if
err
!=
nil
{
return
errors
.
New
(
err
.
Error
())
}
return
nil
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论