优化响应配置

This commit is contained in:
白茶清欢 2024-07-23 17:09:29 +08:00
parent 9eb3b92c41
commit ca0cb31235
5 changed files with 73 additions and 68 deletions

View File

@ -7,31 +7,13 @@
// Date : 2022-06-25 20:33
package define
import (
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/wrapper"
)
var (
// RequestIDField 请求ID
RequestIDField = "request_id"
// TraceIDField 追踪ID
TraceIDField = "trace_id"
// StartRequestTimeField 开始请求时间字段
StartRequestTimeField = "start_request_time"
// FinishRequestTimeField 完成请求时间
FinishRequestTimeField = "finish_request_time"
// ResponseCodeField 响应状态码字段
ResponseCodeField = "code"
// ResponseMessageField 响应信息字段
ResponseMessageField = "message"
// HandleRequestCostField 处理请求耗时
HandleRequestCostField = "cost"
// ResponseDataField 响应数据字段
ResponseDataField = "data"
// ResponseTraceIDField 响应TraceID字段
ResponseTraceIDField = "trace_id"
// ResponseRequestIDField 响应的请求ID字段
ResponseRequestIDField = "request_id"
// RecordResponseDataField 记录响应数据到上下文
RecordResponseDataField = "pkg_gin_record_response_data"
// RecordRequestDataField 记录请求数据到上下文
RecordRequestDataField = "pkg_gin_record_request_data"
inputHttpHandleConfig *HttpHandleConfig
)
// HttpHandleConfig 请求处理配置
@ -47,8 +29,8 @@ type HttpHandleConfig struct {
HandleRequestCostField string
ResponseDataField string
ResponseTraceIDField string
ResponseRequestIDField string
StartRequestTimeField string
FinishRequestTimeField string
}
// ConvertDefaultConfig 覆盖默认配置
@ -57,39 +39,60 @@ type HttpHandleConfig struct {
//
// Date : 20:41 2022/6/25
func ConvertDefaultConfig(cfg *HttpHandleConfig) {
if len(cfg.RequestIDField) > 0 {
RequestIDField = cfg.ResponseRequestIDField
inputHttpHandleConfig = cfg
}
if len(cfg.TraceIDField) > 0 {
TraceIDField = cfg.TraceIDField
}
if len(cfg.ResponseCodeField) > 0 {
ResponseCodeField = cfg.ResponseCodeField
}
if len(cfg.ResponseMessageField) > 0 {
ResponseMessageField = cfg.ResponseMessageField
}
if len(cfg.ResponseDataField) > 0 {
ResponseDataField = cfg.ResponseDataField
}
if len(cfg.ResponseRequestIDField) > 0 {
ResponseRequestIDField = cfg.ResponseRequestIDField
}
if len(cfg.ResponseTraceIDField) > 0 {
ResponseTraceIDField = cfg.ResponseTraceIDField
}
if len(cfg.HandleRequestCostField) > 0 {
HandleRequestCostField = cfg.HandleRequestCostField
}
if len(cfg.StartRequestTimeField) > 0 {
StartRequestTimeField = cfg.StartRequestTimeField
// GetHttpHandleConfig 获取http配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:55 2024/7/23
func GetHttpHandleConfig() *HttpHandleConfig {
return &HttpHandleConfig{
RequestIDField: wrapper.TernaryOperator.String(
nil == inputHttpHandleConfig || inputHttpHandleConfig.RequestIDField == "",
consts.GinRequestIDField,
wrapper.String(inputHttpHandleConfig.RequestIDField),
).Value(),
TraceIDField: wrapper.TernaryOperator.String(
nil == inputHttpHandleConfig || inputHttpHandleConfig.TraceIDField == "",
consts.GinTraceIDField,
wrapper.String(inputHttpHandleConfig.TraceIDField),
).Value(),
ResponseCodeField: wrapper.TernaryOperator.String(
nil == inputHttpHandleConfig || inputHttpHandleConfig.ResponseCodeField == "",
consts.GinResponseCodeField,
wrapper.String(inputHttpHandleConfig.ResponseCodeField),
).Value(),
ResponseMessageField: wrapper.TernaryOperator.String(
nil == inputHttpHandleConfig || inputHttpHandleConfig.ResponseMessageField == "",
consts.GinResponseMessageField,
wrapper.String(inputHttpHandleConfig.ResponseMessageField),
).Value(),
HandleRequestCostField: wrapper.TernaryOperator.String(
nil == inputHttpHandleConfig || inputHttpHandleConfig.HandleRequestCostField == "",
consts.GinHandleRequestCostField,
wrapper.String(inputHttpHandleConfig.HandleRequestCostField),
).Value(),
ResponseDataField: wrapper.TernaryOperator.String(
nil == inputHttpHandleConfig || inputHttpHandleConfig.ResponseDataField == "",
consts.GinResponseDataField,
wrapper.String(inputHttpHandleConfig.ResponseDataField),
).Value(),
ResponseTraceIDField: wrapper.TernaryOperator.String(
nil == inputHttpHandleConfig || inputHttpHandleConfig.ResponseTraceIDField == "",
consts.GinResponseTraceIDField,
wrapper.String(inputHttpHandleConfig.ResponseTraceIDField),
).Value(),
StartRequestTimeField: wrapper.TernaryOperator.String(
nil == inputHttpHandleConfig || inputHttpHandleConfig.StartRequestTimeField == "",
consts.GinStartRequestTimeField,
wrapper.String(inputHttpHandleConfig.StartRequestTimeField),
).Value(),
FinishRequestTimeField: wrapper.TernaryOperator.String(
nil == inputHttpHandleConfig || inputHttpHandleConfig.FinishRequestTimeField == "",
consts.GinFinishRequestTimeField,
wrapper.String(inputHttpHandleConfig.FinishRequestTimeField),
).Value(),
}
}

2
go.mod
View File

@ -16,7 +16,7 @@ require (
)
require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240719075638-afb5d1d933ce // indirect
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240723085016-ee0510753552 // indirect
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 // indirect
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 // indirect
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd // indirect

2
go.sum
View File

@ -1,5 +1,7 @@
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240719075638-afb5d1d933ce h1:DH01rT/F/UNMqXm5HIv+Q2oKPSkg8rjy1TK8pE83rpU=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240719075638-afb5d1d933ce/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240723085016-ee0510753552 h1:SgxcaLXEXn3ImMOik9y3xDz82KwK/+9IhqV1kZbqIwQ=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240723085016-ee0510753552/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U=
git.zhangdeman.cn/zhangdeman/exception v0.0.0-20231105153815-e8561a060cc8 h1:q9pXs8ByVg/XwyDopIGyfEOi/LyHFR0r1rCnJ90uFxw=

View File

@ -1,4 +1,4 @@
// Package gin ...
// Package request ...
//
// Description : gin ...
//

View File

@ -43,18 +43,18 @@ func Send(ctx *gin.Context, code interface{}, httpCode int, data interface{}) {
}
// 设置数据已发送的标识
defer ctx.Set(hasSendResponseFlag, true)
responseConfig := define.GetHttpHandleConfig()
finishRequestTime := time.Now()
responseData := map[string]interface{}{
define.ResponseCodeField: code,
define.ResponseMessageField: exception.GetMessage(code),
define.ResponseTraceIDField: ctx.GetString(define.TraceIDField),
define.ResponseRequestIDField: ctx.GetString(define.RequestIDField),
define.ResponseDataField: data,
define.HandleRequestCostField: finishRequestTime.UnixMilli() - ctx.GetTime(define.StartRequestTimeField).UnixMilli(),
responseConfig.ResponseCodeField: code,
responseConfig.ResponseMessageField: exception.GetMessage(code),
responseConfig.ResponseTraceIDField: ctx.GetString(responseConfig.ResponseTraceIDField),
responseConfig.ResponseDataField: data,
responseConfig.HandleRequestCostField: finishRequestTime.UnixMilli() - ctx.GetTime(responseConfig.StartRequestTimeField).UnixMilli(),
}
// 记录完成时间
ctx.Set(define.FinishRequestTimeField, finishRequestTime)
ctx.Set(define.RecordResponseDataField, responseData)
ctx.Set(responseConfig.FinishRequestTimeField, finishRequestTime)
ctx.Set(responseConfig.ResponseDataField, responseData)
responseException := exception.New(code, httpCode, responseData)
ctx.JSON(responseException.GetHttpCode(), responseException.GetData())
}