157 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			157 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Package define ...
 | |
| //
 | |
| // Description : define ...
 | |
| //
 | |
| // Author : go_developer@163.com<白茶清欢>
 | |
| //
 | |
| // Date : 2022-06-25 20:33
 | |
| package define
 | |
| 
 | |
| import (
 | |
| 	"sync"
 | |
| 
 | |
| 	"git.zhangdeman.cn/zhangdeman/consts"
 | |
| 	"git.zhangdeman.cn/zhangdeman/wrapper/op_ternary"
 | |
| )
 | |
| 
 | |
| var (
 | |
| 	inputHttpHandleConfig = &HttpHandleConfig{}
 | |
| )
 | |
| 
 | |
| // HttpHandleConfig 请求处理配置
 | |
| //
 | |
| // Author : go_developer@163.com<白茶清欢>
 | |
| //
 | |
| // Date : 20:41 2022/6/25
 | |
| type HttpHandleConfig struct {
 | |
| 	RecordRequestDataField  string
 | |
| 	RecordResponseDataField string
 | |
| 	RequestIDField          string
 | |
| 	TraceIDField            string
 | |
| 	ResponseCodeField       string
 | |
| 	ResponseMessageField    string
 | |
| 	HandleRequestCostField  string
 | |
| 	ResponseDataField       string
 | |
| 	ResponseTraceIDField    string
 | |
| 	StartRequestTimeField   string
 | |
| 	FinishRequestTimeField  string
 | |
| 	RequestIsSuccessField   string // 请求处理是否成功的标识
 | |
| 	ExtensionOutputField    string // 扩展信息对外输出字段
 | |
| 	EnableExtensionOutput   bool
 | |
| 	DisableDebugStackOutput bool // 禁用异常堆栈打印
 | |
| }
 | |
| 
 | |
| // ConvertDefaultConfig 覆盖默认配置
 | |
| //
 | |
| // Author : go_developer@163.com<白茶清欢>
 | |
| //
 | |
| // Date : 20:41 2022/6/25
 | |
| func ConvertDefaultConfig(cfg *HttpHandleConfig) {
 | |
| 	inputHttpHandleConfig = cfg
 | |
| }
 | |
| 
 | |
| // GetHttpHandleConfig 获取http配置
 | |
| //
 | |
| // Author : go_developer@163.com<白茶清欢>
 | |
| //
 | |
| // Date : 16:55 2024/7/23
 | |
| func GetHttpHandleConfig() *HttpHandleConfig {
 | |
| 	return &HttpHandleConfig{
 | |
| 		EnableExtensionOutput:   inputHttpHandleConfig.EnableExtensionOutput,
 | |
| 		DisableDebugStackOutput: inputHttpHandleConfig.DisableDebugStackOutput,
 | |
| 		RequestIDField: op_ternary.BaseType[string](
 | |
| 			nil == inputHttpHandleConfig || inputHttpHandleConfig.RequestIDField == "",
 | |
| 			consts.GinRequestIDField,
 | |
| 			inputHttpHandleConfig.RequestIDField,
 | |
| 		),
 | |
| 		TraceIDField: op_ternary.BaseType[string](
 | |
| 			nil == inputHttpHandleConfig || inputHttpHandleConfig.TraceIDField == "",
 | |
| 			consts.GinTraceIDField,
 | |
| 			inputHttpHandleConfig.TraceIDField,
 | |
| 		),
 | |
| 		ResponseCodeField: op_ternary.BaseType[string](
 | |
| 			nil == inputHttpHandleConfig || inputHttpHandleConfig.ResponseCodeField == "",
 | |
| 			consts.GinResponseCodeField,
 | |
| 			inputHttpHandleConfig.ResponseCodeField,
 | |
| 		),
 | |
| 		ResponseMessageField: op_ternary.BaseType[string](
 | |
| 			nil == inputHttpHandleConfig || inputHttpHandleConfig.ResponseMessageField == "",
 | |
| 			consts.GinResponseMessageField,
 | |
| 			inputHttpHandleConfig.ResponseMessageField,
 | |
| 		),
 | |
| 		HandleRequestCostField: op_ternary.BaseType[string](
 | |
| 			nil == inputHttpHandleConfig || inputHttpHandleConfig.HandleRequestCostField == "",
 | |
| 			consts.GinHandleRequestCostField,
 | |
| 			inputHttpHandleConfig.HandleRequestCostField,
 | |
| 		),
 | |
| 		ResponseDataField: op_ternary.BaseType[string](
 | |
| 			nil == inputHttpHandleConfig || inputHttpHandleConfig.ResponseDataField == "",
 | |
| 			consts.GinResponseDataField,
 | |
| 			inputHttpHandleConfig.ResponseDataField,
 | |
| 		),
 | |
| 		ResponseTraceIDField: op_ternary.BaseType[string](
 | |
| 			nil == inputHttpHandleConfig || inputHttpHandleConfig.ResponseTraceIDField == "",
 | |
| 			consts.GinResponseTraceIDField,
 | |
| 			inputHttpHandleConfig.ResponseTraceIDField,
 | |
| 		),
 | |
| 		StartRequestTimeField: op_ternary.BaseType[string](
 | |
| 			nil == inputHttpHandleConfig || inputHttpHandleConfig.StartRequestTimeField == "",
 | |
| 			consts.GinStartRequestTimeField,
 | |
| 			inputHttpHandleConfig.StartRequestTimeField,
 | |
| 		),
 | |
| 		FinishRequestTimeField: op_ternary.BaseType[string](
 | |
| 			nil == inputHttpHandleConfig || inputHttpHandleConfig.FinishRequestTimeField == "",
 | |
| 			consts.GinFinishRequestTimeField,
 | |
| 			inputHttpHandleConfig.FinishRequestTimeField,
 | |
| 		),
 | |
| 		RecordRequestDataField: op_ternary.BaseType[string](
 | |
| 			nil == inputHttpHandleConfig || inputHttpHandleConfig.RecordRequestDataField == "",
 | |
| 			consts.GinRecordRequestDataField,
 | |
| 			inputHttpHandleConfig.RecordRequestDataField,
 | |
| 		),
 | |
| 		RecordResponseDataField: op_ternary.BaseType[string](
 | |
| 			nil == inputHttpHandleConfig || inputHttpHandleConfig.RecordResponseDataField == "",
 | |
| 			consts.GinRecordResponseDataField,
 | |
| 			inputHttpHandleConfig.RecordResponseDataField,
 | |
| 		),
 | |
| 		RequestIsSuccessField: op_ternary.BaseType[string](
 | |
| 			nil == inputHttpHandleConfig || inputHttpHandleConfig.RequestIsSuccessField == "",
 | |
| 			consts.GinRequestIsSuccessField,
 | |
| 			inputHttpHandleConfig.RecordResponseDataField,
 | |
| 		),
 | |
| 		ExtensionOutputField: op_ternary.BaseType[string](
 | |
| 			nil == inputHttpHandleConfig || inputHttpHandleConfig.ExtensionOutputField == "",
 | |
| 			consts.GinResponseExtensionField,
 | |
| 			inputHttpHandleConfig.RecordResponseDataField,
 | |
| 		),
 | |
| 	}
 | |
| }
 | |
| 
 | |
| const (
 | |
| 	LogicAfterResponseKey = "__logic_after_response__"
 | |
| )
 | |
| 
 | |
| type LogicAfterResponse struct {
 | |
| 	SuccessHookFuncList []func()      `json:"-"` // 请求最后需要执行的成功hook函数
 | |
| 	FailureHookFuncList []func()      `json:"-"` // 请求最后需要执行的失败hook函数
 | |
| 	Lock                *sync.RWMutex `json:"-"` // 逻辑锁
 | |
| }
 | |
| 
 | |
| func (logic *LogicAfterResponse) AddSuccessHook(f func()) {
 | |
| 	logic.Lock.Lock()
 | |
| 	defer logic.Lock.Unlock()
 | |
| 	logic.SuccessHookFuncList = append(logic.SuccessHookFuncList, f)
 | |
| }
 | |
| func (logic *LogicAfterResponse) AddFailureHook(f func()) {
 | |
| 	logic.Lock.Lock()
 | |
| 	defer logic.Lock.Unlock()
 | |
| 	logic.FailureHookFuncList = append(logic.FailureHookFuncList, f)
 | |
| }
 | |
| 
 | |
| // ResponseOption 响应的可用选项
 | |
| type ResponseOption struct {
 | |
| 	ContentType string         `json:"content_type"` // 响应的contentType
 | |
| 	Extension   map[string]any `json:"extension"`    // 扩展数据
 | |
| 	XmlName     string         `json:"xml_name"`     // 以xml文件格式响应数据时, Xml文件名(根节点)
 | |
| }
 |