-
Notifications
You must be signed in to change notification settings - Fork 12
/
lu.go
266 lines (227 loc) · 6.35 KB
/
lu.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
package lu
import (
"fmt"
"github.com/valyala/fasthttp"
)
type (
handleFunc func(*fasthttp.RequestCtx, func(error))
errorsFunc func(error, *fasthttp.RequestCtx, func(error))
midware struct {
route []byte
index int
middleWare handleFunc
errorsWare errorsFunc
}
stack []midware
finalResponse func(err error, ctx *fasthttp.RequestCtx)
)
func (stack *stack) push(m midware) {
*stack = append(*stack, m)
}
// Lu is a light-weight web middleware framework
type Lu struct {
// non-error-middleware stack
// store middleware when call app.Use()/Get/Post etc.
stack stack
// error-middleware stack
// store error-middleware when call app.Use()/Get/Post etc.
errStack stack
// when the last error-middleware or non-error-middleware
// calls next(nil) or next(error), there is no more middleware.
// In this case (see func finalRes()):
// 1) if Finally == nil, lu will do below:
// * ctx.ResetBody()
// * ctx.SetStatusCode(fasthttp.StatusNotFound)
// * ctx.SetBodyString("Not Found")
// 2) if Finally is rewrote, how to response to clients by your code.
Finally finalResponse
}
// New a Lu instance
func New() *Lu {
return &Lu{}
}
// Use is use to Register a middleware
// The first parameter of app.Use we call it router,
// and the second parameter we call it middleware,
// all the middlewares will be pushed to stacks inner lu.
//
// In lu, there are two stack arrays to store the midllewares which are accepted
// by app.Use. One is used to store non-error-middleware
// and the other one is used to store error-middleware
//
// func(ctx *fasthttp.RequestCtx, next func(error)),
// we call it non-error-middleware
//
// http request will execute each middleware one-by-one until a middleware
// does not call next(nil) within it.
//
// func(err error, ctx *fasthttp.RequestCtx, next func(error)), we call it
// error-middleware, only execute by call next(error) within a middleware
func (lu *Lu) Use(route string, handler interface{}) {
if route == "" || route[0] != '/' {
panic("The first params of Use func must be a string which start with '/'")
}
if route == "/" {
route = ""
}
midHandle, mOk := handler.(func(*fasthttp.RequestCtx, func(error)))
errHandle, eOk := handler.(func(error, *fasthttp.RequestCtx, func(error)))
if !mOk && !eOk {
panic("The second params of Use func must be a" +
"\n\tfunc(*fasthttp.RequestCtx, func(error)) or" +
"\n\tfunc(error, *fasthttp.RequestCtx, func(error)) type")
}
if mOk {
lu.stack.push(midware{
route: []byte(route),
index: len(lu.errStack),
middleWare: midHandle})
return
}
if eOk {
lu.errStack.push(midware{
route: []byte(route),
index: len(lu.stack),
errorsWare: errHandle})
return
}
}
// Post register a middleware only handle POST method
func (lu *Lu) Post(route string, handler handleFunc) {
lu.httpMethod(route, []byte("POST"), handler)
}
// Put register a middleware only handle PUT method
func (lu *Lu) Put(route string, handler handleFunc) {
lu.httpMethod(route, []byte("PUT"), handler)
}
// Get register a middleware only handle GET method
func (lu *Lu) Get(route string, handler handleFunc) {
lu.httpMethod(route, []byte("GET"), handler)
}
// Delete register a middleware only handle DELETE method
func (lu *Lu) Delete(route string, handler handleFunc) {
lu.httpMethod(route, []byte("DELETE"), handler)
}
// Options register a middleware only handle OPTIONS method
func (lu *Lu) Options(route string, handler handleFunc) {
lu.httpMethod(route, []byte("OPTIONS"), handler)
}
// Patch register a middleware only handle PATCH method
func (lu *Lu) Patch(route string, handler handleFunc) {
lu.httpMethod(route, []byte("PATCH"), handler)
}
// Head register a middleware only handle HEAD method
func (lu *Lu) Head(route string, handler handleFunc) {
lu.httpMethod(route, []byte("HEAD"), handler)
}
// Listen is used to listen a port
func (lu *Lu) Listen(port string) error {
return fasthttp.ListenAndServe(port, lu.Handler)
}
// Handler an incoming http request , lu will compare ctx.Path() with []byte(router),
// The compare rules are below: (see handle())
// * if ctx.Path() equals to []byte(router) , it matches.
// * if ctx.Path() starts with []byte(router), and len(ctx.Path()) > len(router),
// and ctx.Path()[len(router)] is '/' or '?', it matches.
// * if the router is "/",it means this router matches any http request
func (lu *Lu) Handler(ctx *fasthttp.RequestCtx) {
var (
index = 0
errIndex = 0
nxt func(error)
err error
)
nxt = func(err error) {
var m midware
if err != nil {
if errIndex >= len(lu.errStack) {
lu.finalRes(err, ctx)
return
}
m = lu.errStack[errIndex]
errIndex++
index = m.index
} else {
if index >= len(lu.stack) {
lu.finalRes(nil, ctx)
return
}
m = lu.stack[index]
index++
errIndex = m.index
}
handle(err, ctx, m, nxt)
}
nxt(err)
}
// The finalResponse to clients
// see Finally
func (lu *Lu) finalRes(err error, ctx *fasthttp.RequestCtx) {
if lu.Finally != nil {
lu.Finally(err, ctx)
return
}
ctx.ResetBody()
ctx.SetStatusCode(fasthttp.StatusNotFound)
ctx.SetBodyString("Not Found")
}
// Call lu.Use(),
// only handle the given method
func (lu *Lu) httpMethod(route string, method []byte, handler handleFunc) {
lu.Use(route, func(ctx *fasthttp.RequestCtx, next func(error)) {
if sliceCompare(ctx.Method(), method) {
handler(ctx, next)
return
}
next(nil)
})
}
func sliceCompare(src, dest []byte) bool {
if len(src) != len(dest) {
return false
}
return sliceDiff(src, dest)
}
func sliceContains(src, dest []byte) bool {
if len(src) < len(dest) {
return false
}
return sliceDiff(src, dest)
}
func sliceDiff(src, dest []byte) bool {
for i, w := range dest {
if src[i] != w {
return false
}
}
return true
}
// see Handler()
func handle(err error, ctx *fasthttp.RequestCtx, m midware, n func(error)) {
url := ctx.Path()
urlLen := len(url)
rouLen := len(m.route)
if !sliceContains(url, m.route) {
n(err)
return
}
if urlLen > rouLen && url[rouLen] != '/' && url[rouLen] != '?' {
n(err)
return
}
if err != nil {
m.errorsWare(err, ctx, n)
return
}
m.middleWare(ctx, n)
return
}
func init() {
version := "0.0.1"
fmt.Printf(` ___
| / .. )))
| | . . (((
| | . ||~~~~||
| |___ . | \__/ |
\_____/ \____/ ` + "version: " + version + "\n\n")
}