-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.go
More file actions
222 lines (190 loc) · 5.43 KB
/
Copy pathrouter.go
File metadata and controls
222 lines (190 loc) · 5.43 KB
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
package webkit
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
"github.com/dairaga/log"
"github.com/golang/protobuf/proto"
"github.com/gorilla/mux"
)
var (
_result = reflect.TypeOf((*Result)(nil)).Elem()
_respWriter = reflect.TypeOf((*http.ResponseWriter)(nil)).Elem()
_request = reflect.TypeOf((*http.Request)(nil))
_protomsg = reflect.TypeOf((*proto.Message)(nil)).Elem()
)
func _cast(in string, kind reflect.Kind) reflect.Value {
if kind == reflect.String {
return reflect.ValueOf(in)
}
if kind >= reflect.Int && kind <= reflect.Int64 {
a, err := strconv.ParseInt(in, 0, 0)
if err == nil {
switch kind {
case reflect.Int:
return reflect.ValueOf(int(a))
case reflect.Int8:
return reflect.ValueOf(int8(a))
case reflect.Int16:
return reflect.ValueOf(int16(a))
case reflect.Int32:
return reflect.ValueOf(int32(a))
case reflect.Int64:
return reflect.ValueOf(int64(a))
}
}
} else if kind >= reflect.Uint && kind <= reflect.Uint64 {
a, err := strconv.ParseUint(in, 0, 0)
if err == nil {
switch kind {
case reflect.Uint:
return reflect.ValueOf(uint(a))
case reflect.Uint8:
return reflect.ValueOf(uint8(a))
case reflect.Uint16:
return reflect.ValueOf(uint16(a))
case reflect.Uint32:
return reflect.ValueOf(uint32(a))
case reflect.Uint64:
return reflect.ValueOf(uint64(a))
}
}
} else if kind == reflect.Float32 {
a, err := strconv.ParseFloat(in, 32)
if err == nil {
return reflect.ValueOf(float32(a))
}
} else if kind == reflect.Float64 {
a, err := strconv.ParseFloat(in, 64)
if err == nil {
return reflect.ValueOf(a)
}
} else if kind == reflect.Bool {
a, err := strconv.ParseBool(in)
if err == nil {
return reflect.ValueOf(a)
}
}
return reflect.Value{}
}
func _isByteSlice(t reflect.Type) bool {
return t.Kind() == reflect.Slice && t.Elem().Kind() == reflect.Uint8
}
func _isPtrOfStruct(t reflect.Type) bool {
return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct
}
func _indirectType(t reflect.Type) reflect.Type {
elm := t
for elm.Kind() == reflect.Ptr {
elm = elm.Elem()
}
return elm
}
func _readData(r *http.Request, t reflect.Type) (reflect.Value, error) {
if _isByteSlice(t) {
bodybytes, err := ioutil.ReadAll(r.Body)
return reflect.ValueOf(bodybytes), err
}
data := reflect.New(t.Elem())
contentType := r.Header.Get("Content-Type")
if strings.HasPrefix(contentType, "application/json") || strings.HasPrefix(contentType, "application/octet-stream") {
bodybytes, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Error("read body: ", err)
return reflect.Value{}, err
}
if strings.HasPrefix(contentType, "application/json") {
if err := json.Unmarshal(bodybytes, data.Interface()); err != nil {
log.Error("json unmarshal: ", err)
return reflect.Value{}, err
}
} else if t.Implements(_protomsg) {
if err := proto.Unmarshal(bodybytes, data.Interface().(proto.Message)); err != nil {
log.Error("proto unmarshal: ", err)
return reflect.Value{}, err
}
}
} else if strings.HasPrefix(contentType, "multipart/form-data") {
if err := r.ParseMultipartForm(_maxFileSize); err != nil {
log.Error("parse multipart/form-data: ", err)
return reflect.Value{}, err
}
if err := _schema.Decode(data.Interface(), r.PostForm); err != nil {
log.Error("schema decode for multipart/form-data: ", err)
return reflect.Value{}, err
}
} else {
if err := r.ParseForm(); err != nil {
log.Error("parse form: ", err)
return reflect.Value{}, err
}
var formdata url.Values
if r.Method == "GET" {
formdata = r.Form
} else {
formdata = r.PostForm
}
if err := _schema.Decode(data.Interface(), formdata); err != nil {
log.Error("schema decode: ", err)
return reflect.Value{}, err
}
}
return data, nil
}
func _mkHandleFunc(f interface{}, params ...string) http.HandlerFunc {
typ := reflect.TypeOf(f)
if typ.Kind() != reflect.Func {
panic("not func type")
}
if typ.NumIn() < 2 {
panic("func must have two parameters as least")
}
if !typ.In(0).Implements(_respWriter) || typ.In(0).ConvertibleTo(_request) {
panic("func must func(http.ResponseWrier, *http.Request, ...)")
}
if typ.NumIn() > 2+len(params) {
if !_isPtrOfStruct(typ.In(typ.NumIn()-1)) && !_isByteSlice(typ.In(typ.NumIn()-1)) {
panic("last parameter of func must be pointer of struct or slice of byte")
}
}
if typ.NumOut() > 0 && !typ.Out(0).Implements(_result) {
panic("func return type must be Result interface")
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Debugf("remote: %s, method: %s, url: %s", r.RemoteAddr, r.Method, r.URL)
vars := mux.Vars(r)
vals := []reflect.Value{reflect.ValueOf(w), reflect.ValueOf(r)}
for i, x := range params {
tmp, ok := vars[x]
if !ok {
w.WriteHeader(http.StatusNotFound)
return
}
val := _cast(tmp, typ.In(i+2).Kind())
if !val.IsValid() {
log.Errorf("%d: %v cast to %v error", i, x, typ.In(i).Kind())
w.WriteHeader(http.StatusBadRequest)
return
}
vals = append(vals, val)
}
if typ.NumIn() > 2+len(params) {
val, err := _readData(r, typ.In(typ.NumIn()-1))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
if val.IsValid() {
vals = append(vals, val)
}
}
outs := reflect.ValueOf(f).Call(vals)
if len(outs) > 0 && !outs[0].IsNil() {
Display(w, outs[0].Interface().(Result))
}
})
}