/
ghttp_server_service_handler.go
323 lines (296 loc) · 9.5 KB
/
ghttp_server_service_handler.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package ghttp
import (
"bytes"
"context"
"reflect"
"strings"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/os/gstructs"
"github.com/gogf/gf/v2/text/gstr"
)
// BindHandler registers a handler function to server with a given pattern.
//
// Note that the parameter `handler` can be type of:
// 1. func(*ghttp.Request)
// 2. func(context.Context, BizRequest)(BizResponse, error)
func (s *Server) BindHandler(pattern string, handler interface{}) {
var ctx = context.TODO()
funcInfo, err := s.checkAndCreateFuncInfo(handler, "", "", "")
if err != nil {
s.Logger().Fatalf(ctx, `%+v`, err)
}
s.doBindHandler(ctx, doBindHandlerInput{
Prefix: "",
Pattern: pattern,
FuncInfo: funcInfo,
Middleware: nil,
Source: "",
})
}
type doBindHandlerInput struct {
Prefix string
Pattern string
FuncInfo handlerFuncInfo
Middleware []HandlerFunc
Source string
}
// doBindHandler registers a handler function to server with given pattern.
//
// The parameter `pattern` is like:
// /user/list, put:/user, delete:/user, post:/user@goframe.org
func (s *Server) doBindHandler(ctx context.Context, in doBindHandlerInput) {
s.setHandler(ctx, setHandlerInput{
Prefix: in.Prefix,
Pattern: in.Pattern,
HandlerItem: &HandlerItem{
Type: HandlerTypeHandler,
Info: in.FuncInfo,
Middleware: in.Middleware,
Source: in.Source,
},
})
}
// bindHandlerByMap registers handlers to server using map.
func (s *Server) bindHandlerByMap(ctx context.Context, prefix string, m map[string]*HandlerItem) {
for pattern, handler := range m {
s.setHandler(ctx, setHandlerInput{
Prefix: prefix,
Pattern: pattern,
HandlerItem: handler,
})
}
}
// mergeBuildInNameToPattern merges build-in names into the pattern according to the following
// rules, and the built-in names are named like "{.xxx}".
// Rule 1: The URI in pattern contains the {.struct} keyword, it then replaces the keyword with the struct name;
// Rule 2: The URI in pattern contains the {.method} keyword, it then replaces the keyword with the method name;
// Rule 2: If Rule 1 is not met, it then adds the method name directly to the URI in the pattern;
//
// The parameter `allowAppend` specifies whether allowing appending method name to the tail of pattern.
func (s *Server) mergeBuildInNameToPattern(pattern string, structName, methodName string, allowAppend bool) string {
structName = s.nameToUri(structName)
methodName = s.nameToUri(methodName)
pattern = strings.ReplaceAll(pattern, "{.struct}", structName)
if strings.Contains(pattern, "{.method}") {
return strings.ReplaceAll(pattern, "{.method}", methodName)
}
if !allowAppend {
return pattern
}
// Check domain parameter.
var (
array = strings.Split(pattern, "@")
uri = strings.TrimRight(array[0], "/") + "/" + methodName
)
// Append the domain parameter to URI.
if len(array) > 1 {
return uri + "@" + array[1]
}
return uri
}
// nameToUri converts the given name to the URL format using the following rules:
// Rule 0: Convert all method names to lowercase, add char '-' between words.
// Rule 1: Do not convert the method name, construct the URI with the original method name.
// Rule 2: Convert all method names to lowercase, no connecting symbols between words.
// Rule 3: Use camel case naming.
func (s *Server) nameToUri(name string) string {
switch s.config.NameToUriType {
case UriTypeFullName:
return name
case UriTypeAllLower:
return strings.ToLower(name)
case UriTypeCamel:
part := bytes.NewBuffer(nil)
if gstr.IsLetterUpper(name[0]) {
part.WriteByte(name[0] + 32)
} else {
part.WriteByte(name[0])
}
part.WriteString(name[1:])
return part.String()
case UriTypeDefault:
fallthrough
default:
part := bytes.NewBuffer(nil)
for i := 0; i < len(name); i++ {
if i > 0 && gstr.IsLetterUpper(name[i]) {
part.WriteByte('-')
}
if gstr.IsLetterUpper(name[i]) {
part.WriteByte(name[i] + 32)
} else {
part.WriteByte(name[i])
}
}
return part.String()
}
}
func (s *Server) checkAndCreateFuncInfo(
f interface{}, pkgPath, structName, methodName string,
) (funcInfo handlerFuncInfo, err error) {
funcInfo = handlerFuncInfo{
Type: reflect.TypeOf(f),
Value: reflect.ValueOf(f),
}
if handlerFunc, ok := f.(HandlerFunc); ok {
funcInfo.Func = handlerFunc
return
}
var (
reflectType = funcInfo.Type
inputObject reflect.Value
inputObjectPtr interface{}
)
if reflectType.NumIn() != 2 || reflectType.NumOut() != 2 {
if pkgPath != "" {
err = gerror.NewCodef(
gcode.CodeInvalidParameter,
`invalid handler: %s.%s.%s defined as "%s", but "func(*ghttp.Request)" or "func(context.Context, *BizReq)(*BizRes, error)" is required`,
pkgPath, structName, methodName, reflectType.String(),
)
} else {
err = gerror.NewCodef(
gcode.CodeInvalidParameter,
`invalid handler: defined as "%s", but "func(*ghttp.Request)" or "func(context.Context, *BizReq)(*BizRes, error)" is required`,
reflectType.String(),
)
}
return
}
if !reflectType.In(0).Implements(reflect.TypeOf((*context.Context)(nil)).Elem()) {
err = gerror.NewCodef(
gcode.CodeInvalidParameter,
`invalid handler: defined as "%s", but the first input parameter should be type of "context.Context"`,
reflectType.String(),
)
return
}
if !reflectType.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
err = gerror.NewCodef(
gcode.CodeInvalidParameter,
`invalid handler: defined as "%s", but the last output parameter should be type of "error"`,
reflectType.String(),
)
return
}
if reflectType.In(1).Kind() != reflect.Ptr ||
(reflectType.In(1).Kind() == reflect.Ptr && reflectType.In(1).Elem().Kind() != reflect.Struct) {
err = gerror.NewCodef(
gcode.CodeInvalidParameter,
`invalid handler: defined as "%s", but the second input parameter should be type of pointer to struct like "*BizReq"`,
reflectType.String(),
)
return
}
// Do not enable this logic, as many users are already using none struct pointer type
// as the first output parameter.
/*
if reflectType.Out(0).Kind() != reflect.Ptr ||
(reflectType.Out(0).Kind() == reflect.Ptr && reflectType.Out(0).Elem().Kind() != reflect.Struct) {
err = gerror.NewCodef(
gcode.CodeInvalidParameter,
`invalid handler: defined as "%s", but the first output parameter should be type of pointer to struct like "*BizRes"`,
reflectType.String(),
)
return
}
*/
// The request struct should be named as `xxxReq`.
reqStructName := trimGeneric(reflectType.In(1).String())
if !gstr.HasSuffix(reqStructName, `Req`) {
err = gerror.NewCodef(
gcode.CodeInvalidParameter,
`invalid struct naming for request: defined as "%s", but it should be named with "Req" suffix like "XxxReq"`,
reqStructName,
)
return
}
// The response struct should be named as `xxxRes`.
resStructName := trimGeneric(reflectType.Out(0).String())
if !gstr.HasSuffix(resStructName, `Res`) {
err = gerror.NewCodef(
gcode.CodeInvalidParameter,
`invalid struct naming for response: defined as "%s", but it should be named with "Res" suffix like "XxxRes"`,
resStructName,
)
return
}
funcInfo.IsStrictRoute = true
inputObject = reflect.New(funcInfo.Type.In(1).Elem())
inputObjectPtr = inputObject.Interface()
// It retrieves and returns the request struct fields.
fields, err := gstructs.Fields(gstructs.FieldsInput{
Pointer: inputObjectPtr,
RecursiveOption: gstructs.RecursiveOptionEmbedded,
})
if err != nil {
return funcInfo, err
}
funcInfo.ReqStructFields = fields
funcInfo.Func = createRouterFunc(funcInfo)
return
}
func createRouterFunc(funcInfo handlerFuncInfo) func(r *Request) {
return func(r *Request) {
var (
ok bool
err error
inputValues = []reflect.Value{
reflect.ValueOf(r.Context()),
}
)
if funcInfo.Type.NumIn() == 2 {
var inputObject reflect.Value
if funcInfo.Type.In(1).Kind() == reflect.Ptr {
inputObject = reflect.New(funcInfo.Type.In(1).Elem())
r.error = r.Parse(inputObject.Interface())
} else {
inputObject = reflect.New(funcInfo.Type.In(1).Elem()).Elem()
r.error = r.Parse(inputObject.Addr().Interface())
}
if r.error != nil {
return
}
inputValues = append(inputValues, inputObject)
}
// Call handler with dynamic created parameter values.
results := funcInfo.Value.Call(inputValues)
switch len(results) {
case 1:
if !results[0].IsNil() {
if err, ok = results[0].Interface().(error); ok {
r.error = err
}
}
case 2:
r.handlerResponse = results[0].Interface()
if !results[1].IsNil() {
if err, ok = results[1].Interface().(error); ok {
r.error = err
}
}
}
}
}
// trimGeneric removes type definitions string from response type name if generic
func trimGeneric(structName string) string {
var (
leftBraceIndex = strings.LastIndex(structName, "[") // for generic, it is faster to start at the end than at the beginning
rightBraceIndex = strings.LastIndex(structName, "]")
)
if leftBraceIndex == -1 || rightBraceIndex == -1 {
// not found '[' or ']'
return structName
} else if leftBraceIndex+1 == rightBraceIndex {
// may be a slice, because generic is '[X]', not '[]'
// to be compatible with bad return parameter type: []XxxRes
return structName
}
return structName[:leftBraceIndex]
}