/
sabuhp.go
302 lines (242 loc) · 6.74 KB
/
sabuhp.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
package sabuhp
import (
"context"
"net/http"
"time"
"github.com/influx6/npkg/nthen"
"github.com/influx6/npkg"
"github.com/influx6/npkg/njson"
"github.com/influx6/npkg/nnet"
)
type RetryFunc func(last int) time.Duration
type Logger interface {
njson.Logger
}
// Channel represents a generated subscription on a
// topic which provides the giving callback an handler
// to define the point at which the channel should be
// closed and stopped from receiving updates.
type Channel interface {
Topic() string
Group() string
Close()
Err() error
}
// ErrChannel implements the Channel interface
// but has servers purpose to always return an error.
type ErrChannel struct{ Error error }
func (n *ErrChannel) Close() {}
func (n *ErrChannel) Err() error { return n.Error }
// NoopChannel implements the Channel interface
// but has no internal operational capacity.
// It represents a non functioning channel.
type NoopChannel struct{}
func (n NoopChannel) Close() {}
func (n NoopChannel) Err() error { return nil }
type MessageErr interface {
error
StatusCode() int
ShouldAck() bool
}
func WrapErr(err error, shouldAck bool) MessageErr {
return messageErr{
code: 500,
error: err,
shouldAck: shouldAck,
}
}
func WrapErrWithStatusCode(err error, code int, shouldAck bool) MessageErr {
return messageErr{
code: code,
error: err,
shouldAck: shouldAck,
}
}
type messageErr struct {
error
code int
shouldAck bool
}
func (m messageErr) StatusCode() int {
return m.code
}
func (m messageErr) ShouldAck() bool {
return m.shouldAck
}
// Conn defines the connection type which we can retrieve
// and understand the type.
type Conn interface{}
type Params map[string]string
func (h Params) EncodeObject(encoder npkg.ObjectEncoder) {
for key, value := range h {
encoder.String(key, value)
}
}
func (h Params) Get(k string) string {
return h[k]
}
func (h Params) Set(k string, v string) {
h[k] = v
}
func (h Params) Delete(k string) {
delete(h, k)
}
type Header map[string][]string
func (h Header) Get(k string) string {
if values, ok := h[k]; ok {
return values[0]
}
return ""
}
func (h Header) Values(k string) []string {
return h[k]
}
func (h Header) Add(k string, v string) {
h[k] = append(h[k], v)
}
func (h Header) Set(k string, v string) {
h[k] = append([]string{v}, v)
}
func (h Header) Delete(k string) {
delete(h, k)
}
// BytesDecoder transforms a http request into a Message to be
// delivered.
type BytesDecoder interface {
Decode([]byte) (*Message, error)
}
// Matcher is the interface that all Matchers should be implemented
// in order to be registered into the Mux via the `Mux#AddRequestHandler/Match/MatchFunc` functions.
//
// Look the `Mux#AddRequestHandler` for more.
type Matcher interface {
Match(message *Message) bool
}
type Handler interface {
Handle(http.ResponseWriter, *http.Request, Params)
}
var _ Handler = (*HandlerFunc)(nil)
type HandlerFunc func(http.ResponseWriter, *http.Request, Params)
func (h HandlerFunc) Handle(rw http.ResponseWriter, r *http.Request, p Params) {
h(rw, r, p)
}
// HttpMatcher embodies a matcher which indicates if the
// request exclusively belongs to it, hence allowing it
// to divert a giving request to itself.
type HttpMatcher interface {
Handler
Match(http.ResponseWriter, *http.Request, Params)
}
type Transport struct {
Bus MessageBus
Socket Socket
}
func (t Transport) ToBusElseSocket(msg ...Message) {
if t.Bus != nil {
t.Bus.Send(msg...)
return
}
if t.Socket == nil {
return
}
t.Socket.Send(msg...)
}
func (t Transport) ToSocketElseBus(msg ...Message) {
if t.Socket != nil {
t.Socket.Send(msg...)
return
}
if t.Bus == nil {
return
}
t.Bus.Send(msg...)
}
func (t Transport) ToBoth(msg ...Message) {
t.ToBusOnly(msg...)
t.ToSocketOnly(msg...)
}
func (t Transport) ToBusOnly(msg ...Message) {
if t.Bus == nil {
return
}
t.Bus.Send(msg...)
}
func (t Transport) ToSocketOnly(msg ...Message) {
if t.Socket == nil {
return
}
t.Socket.Send(msg...)
}
type TransportResponse interface {
Handle(context.Context, Message, Transport) MessageErr
}
type TransportResponseFunc func(context.Context, Message, Transport) MessageErr
func (t TransportResponseFunc) Handle(ctx context.Context, message Message, tr Transport) MessageErr {
return t(ctx, message, tr)
}
// MessageBus defines what an underline message transport implementation
// like a message bus or rpc connection that can deliver according to
// required semantics of one-to-one and one-to-many.
type MessageBus interface {
Send(data ...Message)
Listen(topic string, grp string, handler TransportResponse) Channel
SendForReply(tm time.Duration, fromTopic Topic, replyGroup string, data ...Message) *nthen.Future
}
type (
// Wrapper is just a type of `func(TransportResponse) TransportResponse`
// which is a common type definition for net/http middlewares.
Wrapper func(response TransportResponse) TransportResponse
// Wrappers contains `Wrapper`s that can be registered and used by a "main route handler".
// Look the `Pre` and `For/ForFunc` functions too.
Wrappers []Wrapper
HttpWrapper func(Handler) Handler
HttpWrappers []HttpWrapper
)
// For registers the wrappers for a specific handler and returns a handler
// that can be passed via the `UseHandle` function.
func (w HttpWrappers) For(main Handler) Handler {
if len(w) > 0 {
for i, lidx := 0, len(w)-1; i <= lidx; i++ {
main = w[lidx-i](main)
}
}
return main
}
// ForFunc registers the wrappers for a specific raw handler function
// and returns a handler that can be passed via the `UseHandle` function.
func (w HttpWrappers) ForFunc(mainFunc HandlerFunc) Handler {
return w.For(mainFunc)
}
// For registers the wrappers for a specific handler and returns a handler
// that can be passed via the `UseHandle` function.
func (w Wrappers) For(main TransportResponse) TransportResponse {
if len(w) > 0 {
for i, lidx := 0, len(w)-1; i <= lidx; i++ {
main = w[lidx-i](main)
}
}
return main
}
// ForFunc registers the wrappers for a specific raw handler function
// and returns a handler that can be passed via the `UseHandle` function.
func (w Wrappers) ForFunc(mainFunc TransportResponseFunc) TransportResponse {
return w.For(mainFunc)
}
type LocationService interface {
Get(ipAddress string) (nnet.Location, error)
}
// Pre starts a chain of handlers for wrapping a "main route handler"
// the registered "middleware" will run before the main handler(see `Wrappers#For/ForFunc`).
//
// Usage:
// mux := muxie.NewMux()
// myMiddlewares := muxie.Pre(myFirstMiddleware, mySecondMiddleware)
// mux.UseHandle("/", myMiddlewares.ForFunc(myMainRouteTransportResponse))
func Pre(middleware ...Wrapper) Wrappers {
return Wrappers(middleware)
}
type LogHandler func([]*Message)
type MessageRouter interface {
TransportResponse
Matcher
}