-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnex.go
143 lines (123 loc) · 2.57 KB
/
nex.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
package nex
import (
"context"
"encoding/json"
"net/http"
)
type (
// ErrorEncoder encode error to response body
ErrorEncoder func(error) interface{}
// ResponseEncoder encode payload to response body
ResponseEncoder func(payload interface{}) interface{}
// StatusCodeEncoder encode error to response status code
StatusCodeEncoder func(error) int
// DefaultErrorMessage wrap error to json
DefaultErrorMessage struct {
Code int `json:"code"`
Error string `json:"error"`
}
// Nex represents a handler that contains a bundle of hooks
Nex struct {
before []BeforeFunc
after []AfterFunc
adapter HandlerAdapter
}
// NexGroup represents a handler group that contains same hooks
NexGroup struct {
before []BeforeFunc
after []AfterFunc
}
)
var (
errorEncoder ErrorEncoder
responseEncoder ResponseEncoder
statusCodeEncoder StatusCodeEncoder
)
func fail(w http.ResponseWriter, err error) {
w.WriteHeader(statusCodeEncoder(err))
json.NewEncoder(w).Encode(errorEncoder(err))
}
func succ(w http.ResponseWriter, data interface{}) {
json.NewEncoder(w).Encode(responseEncoder(data))
}
func (n *Nex) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
var (
ctx context.Context = context.Background()
err error
resp interface{}
)
// global before middleware
for _, b := range globalBefore {
ctx, err = b(ctx, r)
if err != nil {
fail(w, err)
return
}
}
// before middleware
for _, b := range n.before {
ctx, err = b(ctx, r)
if err != nil {
fail(w, err)
return
}
}
// adapter handler
ctx, resp, err = n.adapter.Invoke(ctx, w, r)
if err != nil {
fail(w, err)
return
}
// after middleware
for _, a := range n.after {
ctx, err = a(ctx, w)
if err != nil {
fail(w, err)
return
}
}
// global after middleware
for _, a := range globalAfter {
ctx, err = a(ctx, w)
if err != nil {
fail(w, err)
return
}
}
if err != nil {
fail(w, err)
} else {
succ(w, resp)
}
}
func (n *Nex) Before(before ...BeforeFunc) *Nex {
for _, b := range before {
if b != nil {
n.before = append(n.before, b)
}
}
return n
}
func (n *Nex) After(after ...AfterFunc) *Nex {
for _, a := range after {
if a != nil {
n.after = append(n.after, a)
}
}
return n
}
func init() {
errorEncoder = func(err error) interface{} {
return &DefaultErrorMessage{
Code: -1,
Error: err.Error(),
}
}
responseEncoder = func(payload interface{}) interface{} {
return payload
}
statusCodeEncoder = func(error) int {
return http.StatusBadRequest
}
}