Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
6bcf8762
提交
6bcf8762
authored
6月 16, 2025
作者:
mooncake9527
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update
上级
739d10d6
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
29 行增加
和
13 行删除
+29
-13
logging.go
gin/middleware/logging.go
+4
-0
response.go
gin/response/response.go
+9
-10
ctx.go
gin/xctx/ctx.go
+16
-3
没有找到文件。
gin/middleware/logging.go
浏览文件 @
6bcf8762
...
@@ -183,6 +183,8 @@ func Logging(opts ...Option) gin.HandlerFunc {
...
@@ -183,6 +183,8 @@ func Logging(opts ...Option) gin.HandlerFunc {
)
)
}
}
c
.
Set
(
ctxUtil
.
KeyReqBody
,
buf
.
Bytes
())
reqID
:=
""
reqID
:=
""
if
o
.
traceIDFrom
==
1
{
if
o
.
traceIDFrom
==
1
{
if
v
,
isExist
:=
c
.
Get
(
ctxUtil
.
ContextTraceIDKey
);
isExist
{
if
v
,
isExist
:=
c
.
Get
(
ctxUtil
.
ContextTraceIDKey
);
isExist
{
...
@@ -209,6 +211,8 @@ func Logging(opts ...Option) gin.HandlerFunc {
...
@@ -209,6 +211,8 @@ func Logging(opts ...Option) gin.HandlerFunc {
// processing requests
// processing requests
c
.
Next
()
c
.
Next
()
//c.Set(ctxUtil.KeyRspBody, newWriter.body.Bytes())
// print return message after processing
// print return message after processing
fields
=
[]
zap
.
Field
{
fields
=
[]
zap
.
Field
{
zap
.
Int
(
"code"
,
c
.
Writer
.
Status
()),
zap
.
Int
(
"code"
,
c
.
Writer
.
Status
()),
...
...
gin/response/response.go
浏览文件 @
6bcf8762
...
@@ -3,14 +3,12 @@ package response
...
@@ -3,14 +3,12 @@ package response
import
(
import
(
"encoding/json"
"encoding/json"
"fmt"
"net/http"
"net/http"
"time"
"time"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
"gitlab.wanzhuangkj.com/tush/xpkg/errcode"
"gitlab.wanzhuangkj.com/tush/xpkg/errcode"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/xctx"
ctxUtil
"gitlab.wanzhuangkj.com/tush/xpkg/gin/xctx"
ctxUtil
"gitlab.wanzhuangkj.com/tush/xpkg/gin/xctx"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
...
@@ -32,7 +30,7 @@ func newResp(c *gin.Context, code int, msg string, data interface{}) *Result {
...
@@ -32,7 +30,7 @@ func newResp(c *gin.Context, code int, msg string, data interface{}) *Result {
Code
:
code
,
Code
:
code
,
Msg
:
msg
,
Msg
:
msg
,
TimeStamp
:
time
.
Now
()
.
Unix
(),
TimeStamp
:
time
.
Now
()
.
Unix
(),
TraceID
:
xctx
.
GetGinCtxTid
(
c
),
TraceID
:
ctxUtil
.
GetGinCtxTid
(
c
),
}
}
// ensure that the data field is not nil on return, note that it is not nil when resp.data=[]interface {}, it is serialized to null
// ensure that the data field is not nil on return, note that it is not nil when resp.data=[]interface {}, it is serialized to null
...
@@ -59,14 +57,15 @@ func writeContentType(w http.ResponseWriter, value []string) {
...
@@ -59,14 +57,15 @@ 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 {
if
res
!=
nil
{
// resBytes, _ := json.Marshal(res)
resBytes
,
_
:=
json
.
Marshal
(
res
)
// c.Set(xctx.KeyResp, resBytes)
c
.
Set
(
ctxUtil
.
KeyRspBody
,
resBytes
)
//}
c
.
Writer
.
Write
(
resBytes
)
err
:=
json
.
NewEncoder
(
c
.
Writer
)
.
Encode
(
res
)
if
err
!=
nil
{
fmt
.
Printf
(
"json encode error, err = %s
\n
"
,
err
.
Error
())
}
}
//err := json.NewEncoder(c.Writer).Encode(res)
//if err != nil {
// fmt.Printf("json encode error, err = %s\n", err.Error())
//}
}
}
func
respJSONWithStatusCode
(
c
*
gin
.
Context
,
code
int
,
msg
string
,
data
...
interface
{})
{
func
respJSONWithStatusCode
(
c
*
gin
.
Context
,
code
int
,
msg
string
,
data
...
interface
{})
{
...
...
gin/xctx/ctx.go
浏览文件 @
6bcf8762
...
@@ -23,6 +23,8 @@ var (
...
@@ -23,6 +23,8 @@ var (
HeaderXTimestampKey
=
"Timestamp"
HeaderXTimestampKey
=
"Timestamp"
KeyReqBody
=
"req-body"
KeyRspBody
=
"rsp-body"
KeyTid
=
"tid"
KeyTid
=
"tid"
KeyClientIP
=
"client_ip"
KeyClientIP
=
"client_ip"
KeyUID
=
"userId"
KeyUID
=
"userId"
...
@@ -33,7 +35,7 @@ var (
...
@@ -33,7 +35,7 @@ var (
KeyToken
=
"token"
KeyToken
=
"token"
KeyUser
=
"user"
KeyUser
=
"user"
KeyResp
=
"resp"
//
KeyResp = "resp"
)
)
var
(
var
(
...
@@ -205,8 +207,19 @@ func getCtxVal[T any](c context.Context, key string) T {
...
@@ -205,8 +207,19 @@ func getCtxVal[T any](c context.Context, key string) T {
return
t
return
t
}
}
func
GetResp
(
c
*
gin
.
Context
)
[]
byte
{
func
GetGinReq
(
c
*
gin
.
Context
)
[]
byte
{
v
,
ok1
:=
c
.
Get
(
KeyResp
)
v
,
ok1
:=
c
.
Get
(
KeyReqBody
)
if
ok1
{
ret
,
ok2
:=
v
.
([]
byte
)
if
ok2
{
return
ret
}
}
return
nil
}
func
GetGinRsp
(
c
*
gin
.
Context
)
[]
byte
{
v
,
ok1
:=
c
.
Get
(
KeyRspBody
)
if
ok1
{
if
ok1
{
ret
,
ok2
:=
v
.
([]
byte
)
ret
,
ok2
:=
v
.
([]
byte
)
if
ok2
{
if
ok2
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论