代码拉取完成,页面将自动刷新
package goo
import (
"sync"
)
var interceptor *Interceptor
var onceInterceptor sync.Once
type Interceptor struct {
interceptors map[string][]func(*Invocation)
globalInterceptors []func(*Invocation)
_interceptor *_Interceptor
_interceptor2 *_Interceptor2
_interceptor3 *_Interceptor3
}
func InterceptorInstance() *Interceptor {
onceInterceptor.Do(func() {
interceptor = &Interceptor{interceptors: make(map[string][]func(*Invocation)),
globalInterceptors: make([]func(*Invocation), 0),
_interceptor: &_Interceptor{}, _interceptor2: &_Interceptor2{}, _interceptor3: &_Interceptor3{}}
})
return interceptor
}
func (it *Interceptor) AddInterceptor(pattern string, interceptors []func(*Invocation)) {
if interceptors == nil || len(interceptors) == 0 {
return
}
thisInterceptors, ok := it.interceptors[pattern]
if !ok {
thisInterceptors = make([]func(*Invocation), 0)
}
thisInterceptors = append(thisInterceptors, interceptors...)
it.interceptors[pattern] = thisInterceptors
}
func (it *Interceptor) AddInterceptorBefore(pattern string, interceptors []func(*Invocation)) {
if interceptors == nil || len(interceptors) == 0 {
return
}
thisInterceptors, ok := it.interceptors[pattern]
if !ok {
thisInterceptors = make([]func(*Invocation), 0)
}
thisInterceptors = append(interceptors, thisInterceptors...)
it.interceptors[pattern] = thisInterceptors
}
func (it *Interceptor) AddGlobalIntercept(interceptors ...func(*Invocation)) {
if interceptors == nil || len(interceptors) == 0 {
return
}
it.globalInterceptors = append(it.globalInterceptors, interceptors...)
}
func (it *Interceptor) GetInterceptors(pattern string) []func(*Invocation) {
its, ok := it.interceptors[pattern]
if ok {
return append(it.globalInterceptors, its...)
}
return it.globalInterceptors
}
type Invocation struct {
Ctx *Context
Handle func(ctx *Context)
Interceptor func(inv *Invocation)
invocation *Invocation
}
func (inv *Invocation) Invoke() {
if inv.Interceptor != nil {
inv.Interceptor(inv.invocation)
} else if inv.Handle != nil {
inv.Handle(inv.Ctx)
}
}
type _Interceptor struct{}
type _Interceptor2 struct{}
type _Interceptor3 struct{}
//route.Add(..).Intercept
func (it *_Interceptor) Intercept(interceptor ...func(*Invocation)) {
patterns := RoutesInstance().controllerMethodPattern
for _, p := range patterns {
InterceptorInstance().AddInterceptorBefore(p, interceptor)
}
RoutesInstance().controllerMethodPattern = make([]string, 0)
}
//web.Get(..).Intercept
func (it *_Interceptor2) Intercept(interceptor ...func(*Invocation)) {
pattern := RoutesInstance().pattern
InterceptorInstance().AddInterceptor(pattern, interceptor)
}
//web.Intercept
func (it *_Interceptor3) Intercept(interceptors ...func(*Invocation)) {
if RoutesInstance().inControllerMethod {
controllerInterceptor := RoutesInstance().controllerInterceptor
controllerInterceptor = append(controllerInterceptor, interceptors...)
RoutesInstance().controllerInterceptor = controllerInterceptor
} else {
InterceptorInstance().AddGlobalIntercept(interceptors...)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。