-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
utils.go
43 lines (35 loc) · 1002 Bytes
/
utils.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
package atreugo
import "github.com/valyala/fasthttp"
// index returns the first index of the target string `t`, or
// -1 if no match is found.
func indexOf(vs []string, t string) int {
for i, v := range vs {
if v == t {
return i
}
}
return -1
}
// include returns `true` if the target string t is in the
// slice.
func include(vs []string, t string) bool {
return indexOf(vs, t) >= 0
}
// execMiddlewares execute all the middlewares functions with the request context given
func execMiddlewares(ctx *RequestCtx, middlewares []Middleware) (int, error) {
for _, middlewareFn := range middlewares {
if statusCode, err := middlewareFn(ctx); err != nil {
return statusCode, err
}
}
return fasthttp.StatusOK, nil
}
func viewToHandler(view View) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
actx := acquireRequestCtx(ctx)
if err := view(actx); err != nil {
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
}
releaseRequestCtx(actx)
}
}