-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux.go
178 lines (150 loc) · 4.74 KB
/
mux.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
package art
import (
"strconv"
)
// NewMux
// If routeDelimiter is an empty string, Message.RouteParam cannot be used.
// RouteDelimiter can only be set to a string of length 1.
// This parameter determines different parts of the Message.Subject.
func NewMux(routeDelimiter string) *Mux {
if len(routeDelimiter) > 1 {
panic("routeDelimiter can only be set to a string of length 1.")
}
var mux = &Mux{
node: newTrie(routeDelimiter),
routeDelimiter: routeDelimiter,
}
return mux
}
// Mux refers to a router or multiplexer, which can be used to handle different message.
//
// Message represents a high-level abstraction data structure containing metadata (e.g. header) + body
type Mux struct {
node *trie
routeDelimiter string
errorHandlers []Middleware
enableMessagePool bool
}
// HandleMessage is also a HandleFunc, but with added routing capabilities.
func (mux *Mux) HandleMessage(message *Message, dependency any) (err error) {
if mux.enableMessagePool {
defer PutMessage(message)
}
defer func() {
if mux.errorHandlers != nil {
err = Link(func(message *Message, dep any) error {
return err
}, mux.errorHandlers...)(message, dependency)
}
}()
if mux.node.transform != nil {
return mux.node.handleMessage("", 0, message, dependency)
}
return mux.node.handleMessage(message.Subject, 0, message, dependency)
}
// Middleware
// Before registering handler, middleware must be defined; otherwise, the handler won't be able to use middleware.
func (mux *Mux) Middleware(middlewares ...Middleware) *Mux {
param := ¶mHandler{
middlewares: middlewares,
}
mux.node.addRoute("", 0, param, []Middleware{})
return mux
}
func (mux *Mux) PreMiddleware(handleFuncs ...HandleFunc) *Mux {
param := ¶mHandler{}
for _, h := range handleFuncs {
param.middlewares = append(param.middlewares, h.PreMiddleware())
}
mux.node.addRoute("", 0, param, []Middleware{})
return mux
}
func (mux *Mux) PostMiddleware(handleFuncs ...HandleFunc) *Mux {
param := ¶mHandler{}
for _, h := range handleFuncs {
param.middlewares = append(param.middlewares, h.PostMiddleware())
}
mux.node.addRoute("", 0, param, []Middleware{})
return mux
}
// Transform
// Originally, the message passed through the mux would only call 'getSubject' once.
// However, if there is a definition of Transform,
// when the message passes through the Transform function, 'getSubject' will be called again.
func (mux *Mux) Transform(transform HandleFunc) *Mux {
param := ¶mHandler{
transform: transform,
}
mux.node.addRoute("", 0, param, []Middleware{})
return mux
}
func (mux *Mux) Handler(subject string, h HandleFunc, mw ...Middleware) *Mux {
param := ¶mHandler{
handler: h,
}
if mw != nil {
param.handler = Link(param.handler, mw...)
param.handlerName = functionName(h)
}
mux.node.addRoute(subject, 0, param, []Middleware{})
return mux
}
func (mux *Mux) HandlerByNumber(subject int, h HandleFunc, mw ...Middleware) *Mux {
return mux.Handler(strconv.Itoa(subject)+mux.routeDelimiter, h, mw...)
}
func (mux *Mux) Group(groupName string) *Mux {
groupNode := mux.node.addRoute(groupName, 0, nil, []Middleware{})
return &Mux{
node: groupNode,
routeDelimiter: mux.routeDelimiter,
errorHandlers: mux.errorHandlers,
enableMessagePool: mux.enableMessagePool,
}
}
func (mux *Mux) GroupByNumber(groupName int) *Mux {
return mux.Group(strconv.Itoa(groupName) + mux.routeDelimiter)
}
// DefaultHandler
// When a subject cannot be found, execute the 'Default'.
//
// "The difference between 'Default' and 'NotFound' is
// that the 'Default' handler will utilize middleware,
// whereas 'NotFound' won't use middleware."
func (mux *Mux) DefaultHandler(h HandleFunc, mw ...Middleware) *Mux {
param := ¶mHandler{
defaultHandler: h,
}
if mw != nil {
param.defaultHandler = Link(param.defaultHandler, mw...)
param.defaultHandlerName = functionName(h)
}
mux.node.addRoute("", 0, param, []Middleware{})
return mux
}
// NotFoundHandler
// When a subject cannot be found, execute the 'NotFound'.
//
// "The difference between 'Default' and 'NotFound' is
// that the 'Default' handler will utilize middleware,
// whereas 'NotFound' won't use middleware."
func (mux *Mux) NotFoundHandler(h HandleFunc) *Mux {
param := ¶mHandler{
notFoundHandler: h,
}
mux.node.addRoute("", 0, param, []Middleware{})
return mux
}
func (mux *Mux) ErrorHandler(errHandlers ...Middleware) *Mux {
mux.errorHandlers = append(mux.errorHandlers, errHandlers...)
return mux
}
func (mux *Mux) EnableMessagePool() *Mux {
mux.enableMessagePool = true
return mux
}
// Endpoints get register handler function information
func (mux *Mux) Endpoints(action func(subject, handler string)) {
for _, v := range mux.node.endpoint() {
action(v[0], v[1])
}
}