forked from savsgio/atreugo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
66 lines (52 loc) · 1.27 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package atreugo
import (
"fmt"
"reflect"
"runtime"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/prefork"
)
func panicf(s string, args ...interface{}) {
panic(fmt.Sprintf(s, args...))
}
func viewToHandler(view View, errorView ErrorView) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
actx := AcquireRequestCtx(ctx)
if err := view(actx); err != nil {
errorView(actx, err, fasthttp.StatusInternalServerError)
}
ReleaseRequestCtx(actx)
}
}
func isEqual(v1, v2 interface{}) bool {
return reflect.ValueOf(v1).Pointer() == reflect.ValueOf(v2).Pointer()
}
func isNil(v interface{}) bool {
return reflect.ValueOf(v).IsNil()
}
func middlewaresInclude(ms []Middleware, fn Middleware) bool {
for _, m := range ms {
if isEqual(m, fn) {
return true
}
}
return false
}
func appendMiddlewares(dst, src []Middleware, skip ...Middleware) []Middleware {
for _, fn := range src {
if !middlewaresInclude(skip, fn) {
dst = append(dst, fn)
}
}
return dst
}
func newPreforkServerBase(s *Atreugo) *prefork.Prefork {
p := &prefork.Prefork{
Network: s.cfg.Network,
Reuseport: s.cfg.Reuseport,
RecoverThreshold: runtime.GOMAXPROCS(0) / 2,
Logger: s.cfg.Logger,
ServeFunc: s.Serve,
}
return p
}