-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathscope.go
121 lines (93 loc) · 2.7 KB
/
scope.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package impl
import (
"errors"
"sync"
"github.com/anonymous5l/goflow/general"
"github.com/anonymous5l/goflow/interfaces"
"github.com/valyala/fasthttp"
)
type ScopeImpl struct {
context *ContextImpl
amiddleware []*FlowContext
bmiddleware []*FlowContext
router map[general.ContextHandleMethod]map[string][]*FlowContext
mutex *sync.RWMutex
Disposed bool
}
var ErrFlowInner = errors.New("flow exception")
func NewScopeImpl(ctx *ContextImpl) *ScopeImpl {
scope := new(ScopeImpl)
scope.context = ctx
scope.mutex = &sync.RWMutex{}
scope.router = make(map[general.ContextHandleMethod]map[string][]*FlowContext)
return scope
}
func (scope *ScopeImpl) Register(method general.ContextHandleMethod, path string, handle func(interfaces.Request) error) error {
scope.mutex.Lock()
_, ok := scope.router[method]
if !ok {
scope.router[method] = make(map[string][]*FlowContext)
}
arr, _ := scope.router[method][path]
scope.router[method][path] = append(arr, &FlowContext{
handle: handle,
})
scope.mutex.Unlock()
return nil
}
func (scope *ScopeImpl) Before(handle func(interfaces.Request) error) error {
scope.mutex.Lock()
scope.bmiddleware = append(scope.bmiddleware, &FlowContext{
handle: handle,
})
scope.mutex.Unlock()
return nil
}
func (scope *ScopeImpl) After(handle func(interfaces.Request) error) error {
scope.mutex.Lock()
scope.amiddleware = append(scope.amiddleware, &FlowContext{
handle: handle,
})
scope.mutex.Unlock()
return nil
}
func (scope *ScopeImpl) handleArray(flows []*FlowContext, handle *fasthttp.RequestCtx, request interfaces.Request, m string, p string) error {
defer func() {
scope.mutex.RUnlock()
}()
scope.mutex.RLock()
for _, m := range flows {
err := m.handle(request)
if err == general.End {
break
}
if err == general.Abort {
return err
}
}
return nil
}
func (scope *ScopeImpl) HandleBefore(handle *fasthttp.RequestCtx, request interfaces.Request, m string, p string) error {
return scope.handleArray(scope.bmiddleware, handle, request, m, p)
}
func (scope *ScopeImpl) HandleAfter(handle *fasthttp.RequestCtx, request interfaces.Request, m string, p string) error {
return scope.handleArray(scope.amiddleware, handle, request, m, p)
}
func (scope *ScopeImpl) Handle(handle *fasthttp.RequestCtx, request interfaces.Request, m string, p string) error {
if d, ok := scope.router[general.ContextHandleMethod(m)]; ok {
if farry, ok := d[p]; ok {
if err := scope.handleArray(farry, handle, request, m, p); err != nil {
return err
}
}
}
return nil
}
func (scope *ScopeImpl) Dispose() {
scope.mutex.Lock()
scope.amiddleware = nil
scope.bmiddleware = nil
scope.router = nil
scope.Disposed = true
scope.mutex.Unlock()
}