-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
228 lines (195 loc) · 5.19 KB
/
middleware.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package xroute
import (
"fmt"
"net/http"
"strings"
"sync"
"github.com/moisespsena-go/topsort"
)
const (
DUPLICATION_OVERRIDE = 0
DUPLICATION_ABORT = 1
DUPLICATION_SKIP = 2
)
// Middlewares type is a slice of standard middleware handlers with methods
// to compose middleware chains and interface{}'s.
type Middlewares []*Middleware
func (this *Middlewares) Add(md ...*Middleware) {
*this = append(*this, md...)
}
type Middleware struct {
Name string
Handler func(chain *ChainHandler)
Before []string
After []string
}
func NewMiddleware(f interface{}) *Middleware {
switch ft := f.(type) {
case func(chain *ChainHandler):
return &Middleware{Handler: ft}
case *Middleware:
return ft
case func(http.Handler) http.Handler:
return &Middleware{Handler: func(chain *ChainHandler) {
ft(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
chain.Next(w, r)
})).ServeHTTP(chain.Writer, chain.request)
}}
default:
panic(fmt.Errorf("Invalid Middleware: %s", f))
}
}
type MiddlewaresStack struct {
Name string
ByName map[string]*Middleware
Items Middlewares
Anonymous Middlewares
acceptAnonymous bool
Len int
mu sync.Mutex
}
func NewMiddlewaresStack(name string, acceptAnonymous bool) *MiddlewaresStack {
return &MiddlewaresStack{
Name: name,
ByName: make(map[string]*Middleware),
acceptAnonymous: acceptAnonymous,
}
}
func (stack *MiddlewaresStack) Copy() *MiddlewaresStack {
byName := make(map[string]*Middleware)
for key, md := range stack.ByName {
byName[key] = md
}
anonymous := make(Middlewares, len(stack.Anonymous))
copy(anonymous, stack.Anonymous)
items := make(Middlewares, len(stack.Items))
copy(items, stack.Items)
return &MiddlewaresStack{
Name: stack.Name,
ByName: byName,
Items: items,
Anonymous: anonymous,
acceptAnonymous: stack.acceptAnonymous,
Len: stack.Len,
}
}
func (stack *MiddlewaresStack) All() (items []*Middleware) {
if stack.Items != nil {
return stack.Items
}
items = append(items, stack.Anonymous...)
for _, md := range stack.ByName {
items = append(items, md)
}
return
}
func (stack *MiddlewaresStack) Override(items Middlewares, option int) *MiddlewaresStack {
return NewMiddlewaresStack(stack.Name, stack.acceptAnonymous).Add(items, option)
}
func (stack *MiddlewaresStack) Has(name ...string) bool {
for _, n := range name {
if _, ok := stack.ByName[n]; !ok {
return false
}
}
return true
}
func (stack *MiddlewaresStack) AddInterface(items []interface{}, option int) *MiddlewaresStack {
var mds Middlewares
for _, item := range items {
switch it := item.(type) {
case Middlewares:
mds = append(mds, it...)
case []*Middleware:
mds = append(mds, it...)
default:
mds = append(mds, NewMiddleware(item))
}
}
return stack.Add(mds, option)
}
func (stack *MiddlewaresStack) Add(items Middlewares, option int) *MiddlewaresStack {
if stack.ByName == nil {
stack.ByName = make(map[string]*Middleware)
}
for i, md := range items {
if md.Name == "" {
if stack.acceptAnonymous {
stack.Anonymous = append(stack.Anonymous, md)
stack.Len++
} else {
panic(fmt.Errorf("%v[%v]: Name is empty.", stack.Name, i))
}
} else {
if stack.Has(md.Name) {
switch option {
case DUPLICATION_ABORT:
panic(fmt.Errorf("%v: %q has be registered.", stack.Name, md.Name))
case DUPLICATION_SKIP:
continue
case DUPLICATION_OVERRIDE:
default:
panic(fmt.Errorf("%v: Invalid interseptor option %v.", option))
}
}
stack.ByName[md.Name] = md
stack.Len++
}
}
return stack
}
func (stack *MiddlewaresStack) Build() *MiddlewaresStack {
if len(stack.Items) == stack.Len {
return stack
}
stack.mu.Lock()
defer stack.mu.Unlock()
notFound := make(map[string][]string)
graph := topsort.NewGraph()
for _, md := range stack.ByName {
graph.AddNode(md.Name)
}
for _, md := range stack.ByName {
for _, to := range md.Before {
if stack.Has(to) {
graph.AddEdge(md.Name, to)
} else {
if _, ok := notFound[md.Name]; !ok {
notFound[md.Name] = make([]string, 1)
}
notFound[md.Name] = append(notFound[md.Name], to)
}
}
for _, from := range md.After {
if stack.Has(from) {
graph.AddEdge(from, md.Name)
} else {
if _, ok := notFound[md.Name]; !ok {
notFound[md.Name] = make([]string, 1)
}
notFound[md.Name] = append(notFound[md.Name], from)
}
}
}
if len(notFound) > 0 {
var msgs []string
for n, items := range notFound {
msgs = append(msgs, fmt.Sprintf("%v: Required by %q: %v.", stack.Name, n, strings.Join(items, ", ")))
}
panic(fmt.Errorf("qor/route %v: middlewares dependency error:\n - %v\n", stack.Name, strings.Join(msgs, "\n - ")))
}
names, err := graph.DepthFirst()
if err != nil {
panic(fmt.Errorf("qor/route %v: topological middlewares sorter error: %v", stack.Name, err))
}
stack.Items = make(Middlewares, 0)
// named middlewares at begin
for _, name := range names {
stack.Items = append(stack.Items, stack.ByName[name])
}
// named middlewares at end
for _, md := range stack.Anonymous {
stack.Items = append(stack.Items, md)
}
return stack
}