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