forked from chanxuehong/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
346 lines (309 loc) · 9.96 KB
/
handler.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package core
import (
"github.com/chanxuehong/wechat.v2/internal/util"
)
const maxHandlerChainSize = 64
type HandlerChain []Handler
type Handler interface {
ServeMsg(*Context)
}
// HandlerFunc =========================================================================================================
var _ Handler = HandlerFunc(nil)
type HandlerFunc func(*Context)
func (fn HandlerFunc) ServeMsg(ctx *Context) { fn(ctx) }
// ServeMux ============================================================================================================
var _ Handler = (*ServeMux)(nil)
// ServeMux 是一个消息(事件)路由器, 同时也是一个 Handler 的实现.
// NOTE: ServeMux 非并发安全, 如果需要并发安全的 Handler, 可以参考 ServeMux 实现一个.
type ServeMux struct {
startedChecker startedChecker
msgMiddlewares HandlerChain
eventMiddlewares HandlerChain
defaultMsgHandlerChain HandlerChain
defaultEventHandlerChain HandlerChain
msgHandlerChainMap map[MsgType]HandlerChain
eventHandlerChainMap map[EventType]HandlerChain
}
func NewServeMux() *ServeMux {
return &ServeMux{
msgHandlerChainMap: make(map[MsgType]HandlerChain),
eventHandlerChainMap: make(map[EventType]HandlerChain),
}
}
var successResponseBytes = []byte("success")
// ServeMsg 实现 Handler 接口.
func (mux *ServeMux) ServeMsg(ctx *Context) {
mux.startedChecker.start()
if MsgType := ctx.MixedMsg.MsgType; MsgType != "event" {
handlers := mux.getMsgHandlerChain(MsgType)
if len(handlers) == 0 {
ctx.ResponseWriter.Write(successResponseBytes)
return
}
ctx.handlers = handlers
ctx.Next()
} else {
handlers := mux.getEventHandlerChain(ctx.MixedMsg.EventType)
if len(handlers) == 0 {
ctx.ResponseWriter.Write(successResponseBytes)
return
}
ctx.handlers = handlers
ctx.Next()
}
}
// getMsgHandlerChain 获取 HandlerChain 以处理消息类型为 MsgType 的消息, 如果没有找到返回 nil.
func (mux *ServeMux) getMsgHandlerChain(msgType MsgType) (handlers HandlerChain) {
if m := mux.msgHandlerChainMap; len(m) > 0 {
handlers = m[MsgType(util.ToLower(string(msgType)))]
if len(handlers) == 0 {
handlers = mux.defaultMsgHandlerChain
}
} else {
handlers = mux.defaultMsgHandlerChain
}
return
}
// getEventHandlerChain 获取 HandlerChain 以处理事件类型为 EventType 的事件, 如果没有找到返回 nil.
func (mux *ServeMux) getEventHandlerChain(eventType EventType) (handlers HandlerChain) {
if m := mux.eventHandlerChainMap; len(m) > 0 {
handlers = m[EventType(util.ToLower(string(eventType)))]
if len(handlers) == 0 {
handlers = mux.defaultEventHandlerChain
}
} else {
handlers = mux.defaultEventHandlerChain
}
return
}
// ServeMux: registers HandlerChain ====================================================================================
// Use 注册(新增) middlewares 使其在所有消息(事件)的 Handler 之前处理该处理消息(事件).
func (mux *ServeMux) Use(middlewares ...Handler) {
mux.startedChecker.check()
if len(middlewares) == 0 {
return
}
for _, h := range middlewares {
if h == nil {
panic("handler can not be nil")
}
}
mux.useForMsg(middlewares)
mux.useForEvent(middlewares)
}
// UseFunc 注册(新增) middlewares 使其在所有消息(事件)的 Handler 之前处理该处理消息(事件).
func (mux *ServeMux) UseFunc(middlewares ...func(*Context)) {
mux.startedChecker.check()
if len(middlewares) == 0 {
return
}
for _, h := range middlewares {
if h == nil {
panic("handler can not be nil")
}
}
middlewares2 := make(HandlerChain, len(middlewares))
for i := 0; i < len(middlewares); i++ {
middlewares2[i] = HandlerFunc(middlewares[i])
}
mux.useForMsg(middlewares2)
mux.useForEvent(middlewares2)
}
// UseForMsg 注册(新增) middlewares 使其在所有消息的 Handler 之前处理该处理消息.
func (mux *ServeMux) UseForMsg(middlewares ...Handler) {
mux.startedChecker.check()
if len(middlewares) == 0 {
return
}
for _, h := range middlewares {
if h == nil {
panic("handler can not be nil")
}
}
mux.useForMsg(middlewares)
}
// UseFuncForMsg 注册(新增) middlewares 使其在所有消息的 Handler 之前处理该处理消息.
func (mux *ServeMux) UseFuncForMsg(middlewares ...func(*Context)) {
mux.startedChecker.check()
if len(middlewares) == 0 {
return
}
for _, h := range middlewares {
if h == nil {
panic("handler can not be nil")
}
}
middlewares2 := make(HandlerChain, len(middlewares))
for i := 0; i < len(middlewares); i++ {
middlewares2[i] = HandlerFunc(middlewares[i])
}
mux.useForMsg(middlewares2)
}
func (mux *ServeMux) useForMsg(middlewares []Handler) {
if len(mux.defaultMsgHandlerChain) > 0 || len(mux.msgHandlerChainMap) > 0 {
panic("please call this method before any other methods those registered handlers for message")
}
mux.msgMiddlewares = combineHandlerChain(mux.msgMiddlewares, middlewares)
}
// UseForEvent 注册(新增) middlewares 使其在所有事件的 Handler 之前处理该处理事件.
func (mux *ServeMux) UseForEvent(middlewares ...Handler) {
mux.startedChecker.check()
if len(middlewares) == 0 {
return
}
for _, h := range middlewares {
if h == nil {
panic("handler can not be nil")
}
}
mux.useForEvent(middlewares)
}
// UseFuncForEvent 注册(新增) middlewares 使其在所有事件的 Handler 之前处理该处理事件.
func (mux *ServeMux) UseFuncForEvent(middlewares ...func(*Context)) {
mux.startedChecker.check()
if len(middlewares) == 0 {
return
}
for _, h := range middlewares {
if h == nil {
panic("handler can not be nil")
}
}
middlewares2 := make(HandlerChain, len(middlewares))
for i := 0; i < len(middlewares); i++ {
middlewares2[i] = HandlerFunc(middlewares[i])
}
mux.useForEvent(middlewares2)
}
func (mux *ServeMux) useForEvent(middlewares []Handler) {
if len(mux.defaultEventHandlerChain) > 0 || len(mux.eventHandlerChainMap) > 0 {
panic("please call this method before any other methods those registered handlers for event")
}
mux.eventMiddlewares = combineHandlerChain(mux.eventMiddlewares, middlewares)
}
// DefaultMsgHandle 设置 handlers 以处理没有匹配到具体类型的 HandlerChain 的消息.
func (mux *ServeMux) DefaultMsgHandle(handlers ...Handler) {
mux.startedChecker.check()
if len(handlers) == 0 {
return
}
for _, h := range handlers {
if h == nil {
panic("handler can not be nil")
}
}
mux.defaultMsgHandlerChain = combineHandlerChain(mux.msgMiddlewares, handlers)
}
// DefaultMsgHandleFunc 设置 handlers 以处理没有匹配到具体类型的 HandlerChain 的消息.
func (mux *ServeMux) DefaultMsgHandleFunc(handlers ...func(*Context)) {
mux.startedChecker.check()
if len(handlers) == 0 {
return
}
for _, h := range handlers {
if h == nil {
panic("handler can not be nil")
}
}
handlers2 := make(HandlerChain, len(handlers))
for i := 0; i < len(handlers); i++ {
handlers2[i] = HandlerFunc(handlers[i])
}
mux.defaultMsgHandlerChain = combineHandlerChain(mux.msgMiddlewares, handlers2)
}
// DefaultEventHandle 设置 handlers 以处理没有匹配到具体类型的 HandlerChain 的事件.
func (mux *ServeMux) DefaultEventHandle(handlers ...Handler) {
mux.startedChecker.check()
if len(handlers) == 0 {
return
}
for _, h := range handlers {
if h == nil {
panic("handler can not be nil")
}
}
mux.defaultEventHandlerChain = combineHandlerChain(mux.eventMiddlewares, handlers)
}
// DefaultEventHandleFunc 设置 handlers 以处理没有匹配到具体类型的 HandlerChain 的事件.
func (mux *ServeMux) DefaultEventHandleFunc(handlers ...func(*Context)) {
mux.startedChecker.check()
if len(handlers) == 0 {
return
}
for _, h := range handlers {
if h == nil {
panic("handler can not be nil")
}
}
handlers2 := make(HandlerChain, len(handlers))
for i := 0; i < len(handlers); i++ {
handlers2[i] = HandlerFunc(handlers[i])
}
mux.defaultEventHandlerChain = combineHandlerChain(mux.eventMiddlewares, handlers2)
}
// MsgHandle 设置 handlers 以处理特定类型的消息.
func (mux *ServeMux) MsgHandle(msgType MsgType, handlers ...Handler) {
mux.startedChecker.check()
if len(handlers) == 0 {
return
}
for _, h := range handlers {
if h == nil {
panic("handler can not be nil")
}
}
mux.msgHandlerChainMap[MsgType(util.ToLower(string(msgType)))] = combineHandlerChain(mux.msgMiddlewares, handlers)
}
// MsgHandleFunc 设置 handlers 以处理特定类型的消息.
func (mux *ServeMux) MsgHandleFunc(msgType MsgType, handlers ...func(*Context)) {
mux.startedChecker.check()
if len(handlers) == 0 {
return
}
for _, h := range handlers {
if h == nil {
panic("handler can not be nil")
}
}
handlers2 := make(HandlerChain, len(handlers))
for i := 0; i < len(handlers); i++ {
handlers2[i] = HandlerFunc(handlers[i])
}
mux.msgHandlerChainMap[MsgType(util.ToLower(string(msgType)))] = combineHandlerChain(mux.msgMiddlewares, handlers2)
}
// EventHandle 设置 handlers 以处理特定类型的事件.
func (mux *ServeMux) EventHandle(eventType EventType, handlers ...Handler) {
mux.startedChecker.check()
if len(handlers) == 0 {
return
}
for _, h := range handlers {
if h == nil {
panic("handler can not be nil")
}
}
mux.eventHandlerChainMap[EventType(util.ToLower(string(eventType)))] = combineHandlerChain(mux.eventMiddlewares, handlers)
}
// EventHandleFunc 设置 handlers 以处理特定类型的事件.
func (mux *ServeMux) EventHandleFunc(eventType EventType, handlers ...func(*Context)) {
mux.startedChecker.check()
if len(handlers) == 0 {
return
}
for _, h := range handlers {
if h == nil {
panic("handler can not be nil")
}
}
handlers2 := make(HandlerChain, len(handlers))
for i := 0; i < len(handlers); i++ {
handlers2[i] = HandlerFunc(handlers[i])
}
mux.eventHandlerChainMap[EventType(util.ToLower(string(eventType)))] = combineHandlerChain(mux.eventMiddlewares, handlers2)
}
func combineHandlerChain(middlewares, handlers HandlerChain) HandlerChain {
if len(middlewares)+len(handlers) > maxHandlerChainSize {
panic("too many handlers")
}
return append(middlewares, handlers...)
}