/
ghttp_server_service_handler.go
169 lines (154 loc) · 5.59 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
// 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/rock-rabbit/gf.
package ghttp
import (
"bytes"
"github.com/rock-rabbit/gf/debug/gdebug"
"github.com/rock-rabbit/gf/errors/gcode"
"github.com/rock-rabbit/gf/errors/gerror"
"reflect"
"strings"
"github.com/rock-rabbit/gf/text/gstr"
)
// BindHandler registers a handler function to server with given pattern.
// The parameter `handler` can be type of:
// func(*ghttp.Request)
// func(context.Context)
// func(context.Context,TypeRequest)
// func(context.Context,TypeRequest) error
// func(context.Context,TypeRequest)(TypeResponse,error)
func (s *Server) BindHandler(pattern string, handler interface{}) {
funcInfo, err := s.checkAndCreateFuncInfo(handler, "", "", "")
if err != nil {
s.Logger().Error(err.Error())
return
}
s.doBindHandler(pattern, funcInfo, nil, "")
}
// 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(pattern string, funcInfo handlerFuncInfo, middleware []HandlerFunc, source string) {
s.setHandler(pattern, &handlerItem{
Name: gdebug.FuncPath(funcInfo.Func),
Type: handlerTypeHandler,
Info: funcInfo,
Middleware: middleware,
Source: source,
})
}
// bindHandlerByMap registers handlers to server using map.
func (s *Server) bindHandlerByMap(m map[string]*handlerItem) {
for p, h := range m {
s.setHandler(p, h)
}
}
// 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.Replace(pattern, "{.struct}", structName, -1)
if strings.Index(pattern, "{.method}") != -1 {
return strings.Replace(pattern, "{.method}", methodName, -1)
}
if !allowAppend {
return pattern
}
// Check domain parameter.
array := strings.Split(pattern, "@")
uri := array[0]
uri = strings.TrimRight(uri, "/") + "/" + methodName
// Append the domain parameter to URI.
if len(array) > 1 {
return uri + "@" + array[1]
}
return uri
}
// nameToUri converts the given name to URL format using 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, objName, methodName string) (info handlerFuncInfo, err error) {
handlerFunc, ok := f.(HandlerFunc)
if !ok {
reflectType := reflect.TypeOf(f)
if reflectType.NumIn() == 0 || 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)/func(context.Context,Request)/func(context.Context,Request) error/func(context.Context,Request)(Response,error)" is required`,
pkgPath, objName, methodName, reflect.TypeOf(f).String(),
)
} else {
err = gerror.NewCodef(
gcode.CodeInvalidParameter,
`invalid handler: defined as "%s", but "func(*ghttp.Request)" or "func(context.Context)/func(context.Context,Request)/func(context.Context,Request) error/func(context.Context,Request)(Response,error)" is required`,
reflect.TypeOf(f).String(),
)
}
return
}
if reflectType.In(0).String() != "context.Context" {
err = gerror.NewCodef(
gcode.CodeInvalidParameter,
`invalid handler: defined as "%s", but the first input parameter should be type of "context.Context"`,
reflect.TypeOf(f).String(),
)
return
}
if reflectType.NumOut() > 0 && reflectType.Out(reflectType.NumOut()-1).String() != "error" {
err = gerror.NewCodef(
gcode.CodeInvalidParameter,
`invalid handler: defined as "%s", but the last output parameter should be type of "error"`,
reflect.TypeOf(f).String(),
)
return
}
}
info.Func = handlerFunc
info.Type = reflect.TypeOf(f)
info.Value = reflect.ValueOf(f)
return
}