优化代码组织
This commit is contained in:
parent
b95d464c89
commit
9decd12ee8
@ -10,6 +10,7 @@ package define
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -124,3 +125,24 @@ func GetHttpHandleConfig() *HttpHandleConfig {
|
||||
).Value(),
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
@ -66,24 +66,3 @@ type UriParam struct {
|
||||
const (
|
||||
FieldNameMeta = "Meta" // 元信息字段
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ package router
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/exception"
|
||||
"git.zhangdeman.cn/zhangdeman/gin/define"
|
||||
"git.zhangdeman.cn/zhangdeman/gin/request"
|
||||
"git.zhangdeman.cn/zhangdeman/gin/response"
|
||||
"github.com/gin-gonic/gin"
|
||||
@ -47,13 +48,13 @@ func RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
|
||||
}
|
||||
|
||||
// 初始化响应之后logic
|
||||
logicAfterResponse := &LogicAfterResponse{
|
||||
logicAfterResponse := &define.LogicAfterResponse{
|
||||
SuccessHookFuncList: make([]func(), 0),
|
||||
FailureHookFuncList: make([]func(), 0),
|
||||
Lock: &sync.RWMutex{},
|
||||
}
|
||||
// 此处暴露出去,是为了使用方可以获取到对应数据
|
||||
ctx.Set(LogicAfterResponseKey, logicAfterResponse)
|
||||
ctx.Set(define.LogicAfterResponseKey, logicAfterResponse)
|
||||
|
||||
// 执行逻辑
|
||||
inputValue := reflect.ValueOf(formValue)
|
||||
|
Loading…
x
Reference in New Issue
Block a user