优化响应配置

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 // Date : 2022-06-25 20:33
package define package define
import (
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/wrapper"
)
var ( var (
// RequestIDField 请求ID inputHttpHandleConfig *HttpHandleConfig
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"
) )
// HttpHandleConfig 请求处理配置 // HttpHandleConfig 请求处理配置
@ -47,8 +29,8 @@ type HttpHandleConfig struct {
HandleRequestCostField string HandleRequestCostField string
ResponseDataField string ResponseDataField string
ResponseTraceIDField string ResponseTraceIDField string
ResponseRequestIDField string
StartRequestTimeField string StartRequestTimeField string
FinishRequestTimeField string
} }
// ConvertDefaultConfig 覆盖默认配置 // ConvertDefaultConfig 覆盖默认配置
@ -57,39 +39,60 @@ type HttpHandleConfig struct {
// //
// Date : 20:41 2022/6/25 // Date : 20:41 2022/6/25
func ConvertDefaultConfig(cfg *HttpHandleConfig) { func ConvertDefaultConfig(cfg *HttpHandleConfig) {
if len(cfg.RequestIDField) > 0 { inputHttpHandleConfig = cfg
RequestIDField = cfg.ResponseRequestIDField
} }
if len(cfg.TraceIDField) > 0 { // GetHttpHandleConfig 获取http配置
TraceIDField = cfg.TraceIDField //
} // Author : go_developer@163.com<白茶清欢>
//
if len(cfg.ResponseCodeField) > 0 { // Date : 16:55 2024/7/23
ResponseCodeField = cfg.ResponseCodeField func GetHttpHandleConfig() *HttpHandleConfig {
} return &HttpHandleConfig{
RequestIDField: wrapper.TernaryOperator.String(
if len(cfg.ResponseMessageField) > 0 { nil == inputHttpHandleConfig || inputHttpHandleConfig.RequestIDField == "",
ResponseMessageField = cfg.ResponseMessageField consts.GinRequestIDField,
} wrapper.String(inputHttpHandleConfig.RequestIDField),
).Value(),
if len(cfg.ResponseDataField) > 0 { TraceIDField: wrapper.TernaryOperator.String(
ResponseDataField = cfg.ResponseDataField nil == inputHttpHandleConfig || inputHttpHandleConfig.TraceIDField == "",
} consts.GinTraceIDField,
wrapper.String(inputHttpHandleConfig.TraceIDField),
if len(cfg.ResponseRequestIDField) > 0 { ).Value(),
ResponseRequestIDField = cfg.ResponseRequestIDField ResponseCodeField: wrapper.TernaryOperator.String(
} nil == inputHttpHandleConfig || inputHttpHandleConfig.ResponseCodeField == "",
consts.GinResponseCodeField,
if len(cfg.ResponseTraceIDField) > 0 { wrapper.String(inputHttpHandleConfig.ResponseCodeField),
ResponseTraceIDField = cfg.ResponseTraceIDField ).Value(),
} ResponseMessageField: wrapper.TernaryOperator.String(
nil == inputHttpHandleConfig || inputHttpHandleConfig.ResponseMessageField == "",
if len(cfg.HandleRequestCostField) > 0 { consts.GinResponseMessageField,
HandleRequestCostField = cfg.HandleRequestCostField wrapper.String(inputHttpHandleConfig.ResponseMessageField),
} ).Value(),
HandleRequestCostField: wrapper.TernaryOperator.String(
if len(cfg.StartRequestTimeField) > 0 { nil == inputHttpHandleConfig || inputHttpHandleConfig.HandleRequestCostField == "",
StartRequestTimeField = cfg.StartRequestTimeField 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 ( 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/easymap v0.0.0-20240311030808-e2a2e6a3c211 // indirect
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 // indirect git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 // indirect
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd // 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 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-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 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U= 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= 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 ... // Description : gin ...
// //

View File

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