mirror of
https://gitee.com/johng/gf.git
synced 2024-11-29 18:57:44 +08:00
rename attribute names from lower-camel case to upper-camel case
This commit is contained in:
parent
528f0e5434
commit
f72d991c36
@ -71,35 +71,35 @@ type (
|
||||
// handlerItem is the registered handler for route handling,
|
||||
// including middleware and hook functions.
|
||||
handlerItem struct {
|
||||
itemId int // Unique handler item id mark.
|
||||
itemName string // Handler name, which is automatically retrieved from runtime stack when registered.
|
||||
itemType int // Handler type: object/handler/controller/middleware/hook.
|
||||
itemInfo handlerFuncInfo // Handler function information.
|
||||
initFunc HandlerFunc // Initialization function when request enters the object (only available for object register type).
|
||||
shutFunc HandlerFunc // Shutdown function when request leaves out the object (only available for object register type).
|
||||
middleware []HandlerFunc // Bound middleware array.
|
||||
ctrlInfo *handlerController // Controller information for reflect usage.
|
||||
hookName string // Hook type name, only available for hook type.
|
||||
router *Router // Router object.
|
||||
source string // Registering source file `path:line`.
|
||||
Id int // Unique handler item id mark.
|
||||
Name string // Handler name, which is automatically retrieved from runtime stack when registered.
|
||||
Type int // Handler type: object/handler/controller/middleware/hook.
|
||||
Info handlerFuncInfo // Handler function information.
|
||||
InitFunc HandlerFunc // Initialization function when request enters the object (only available for object register type).
|
||||
ShutFunc HandlerFunc // Shutdown function when request leaves out the object (only available for object register type).
|
||||
Middleware []HandlerFunc // Bound middleware array.
|
||||
CtrlInfo *handlerController // Controller information for reflect usage.
|
||||
HookName string // Hook type name, only available for hook type.
|
||||
Router *Router // Router object.
|
||||
Source string // Registering source file `path:line`.
|
||||
}
|
||||
|
||||
// handlerParsedItem is the item parsed from URL.Path.
|
||||
handlerParsedItem struct {
|
||||
handler *handlerItem // Handler information.
|
||||
values map[string]string // Router values parsed from URL.Path.
|
||||
Handler *handlerItem // Handler information.
|
||||
Values map[string]string // Router values parsed from URL.Path.
|
||||
}
|
||||
|
||||
// handlerController is the controller information used for reflect.
|
||||
handlerController struct {
|
||||
name string // Handler method name.
|
||||
reflect reflect.Type // Reflect type of the controller.
|
||||
Name string // Handler method name.
|
||||
Type reflect.Type // Reflect type of the controller.
|
||||
}
|
||||
|
||||
// registeredRouteItem stores the information of the router and is used for route map.
|
||||
registeredRouteItem struct {
|
||||
source string // Source file path and its line number.
|
||||
handler *handlerItem // Handler object.
|
||||
Source string // Source file path and its line number.
|
||||
Handler *handlerItem // Handler object.
|
||||
}
|
||||
|
||||
// errorStack is the interface for Stack feature.
|
||||
|
@ -11,19 +11,38 @@ import (
|
||||
"github.com/gogf/gf/internal/intlog"
|
||||
)
|
||||
|
||||
type DefaultHandlerResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
// MiddlewareHandlerResponse is the default middleware handling handler response object and its error.
|
||||
func MiddlewareHandlerResponse(r *Request) {
|
||||
r.Middleware.Next()
|
||||
res, err := r.GetHandlerResponse()
|
||||
var (
|
||||
err error
|
||||
res interface{}
|
||||
internalErr error
|
||||
)
|
||||
res, err = r.GetHandlerResponse()
|
||||
if err != nil {
|
||||
r.Response.Writef(
|
||||
`{"code":%d,"message":"%s"}`,
|
||||
gerror.Code(err),
|
||||
err.Error(),
|
||||
)
|
||||
internalErr = r.Response.WriteJson(DefaultHandlerResponse{
|
||||
Code: gerror.Code(err),
|
||||
Message: err.Error(),
|
||||
Data: nil,
|
||||
})
|
||||
if internalErr != nil {
|
||||
intlog.Error(r.Context(), internalErr)
|
||||
}
|
||||
return
|
||||
}
|
||||
if exception := r.Response.WriteJson(res); exception != nil {
|
||||
intlog.Error(r.Context(), exception)
|
||||
internalErr = r.Response.WriteJson(DefaultHandlerResponse{
|
||||
Code: 0,
|
||||
Message: "",
|
||||
Data: res,
|
||||
})
|
||||
if internalErr != nil {
|
||||
intlog.Error(r.Context(), internalErr)
|
||||
}
|
||||
}
|
||||
|
@ -35,20 +35,20 @@ func (m *middleware) Next() {
|
||||
}
|
||||
item = m.request.handlers[m.handlerIndex]
|
||||
// Filter the HOOK handlers, which are designed to be called in another standalone procedure.
|
||||
if item.handler.itemType == handlerTypeHook {
|
||||
if item.Handler.Type == handlerTypeHook {
|
||||
m.handlerIndex++
|
||||
continue
|
||||
}
|
||||
// Current router switching.
|
||||
m.request.Router = item.handler.router
|
||||
m.request.Router = item.Handler.Router
|
||||
|
||||
// Router values switching.
|
||||
m.request.routerMap = item.values
|
||||
m.request.routerMap = item.Values
|
||||
|
||||
gutil.TryCatch(func() {
|
||||
// Execute bound middleware array of the item if it's not empty.
|
||||
if m.handlerMDIndex < len(item.handler.middleware) {
|
||||
md := item.handler.middleware[m.handlerMDIndex]
|
||||
if m.handlerMDIndex < len(item.Handler.Middleware) {
|
||||
md := item.Handler.Middleware[m.handlerMDIndex]
|
||||
m.handlerMDIndex++
|
||||
niceCallFunc(func() {
|
||||
md(m.request)
|
||||
@ -58,20 +58,20 @@ func (m *middleware) Next() {
|
||||
}
|
||||
m.handlerIndex++
|
||||
|
||||
switch item.handler.itemType {
|
||||
switch item.Handler.Type {
|
||||
// Service controller.
|
||||
case handlerTypeController:
|
||||
m.served = true
|
||||
if m.request.IsExited() {
|
||||
break
|
||||
}
|
||||
c := reflect.New(item.handler.ctrlInfo.reflect)
|
||||
c := reflect.New(item.Handler.CtrlInfo.Type)
|
||||
niceCallFunc(func() {
|
||||
c.MethodByName("Init").Call([]reflect.Value{reflect.ValueOf(m.request)})
|
||||
})
|
||||
if !m.request.IsExited() {
|
||||
niceCallFunc(func() {
|
||||
c.MethodByName(item.handler.ctrlInfo.name).Call(nil)
|
||||
c.MethodByName(item.Handler.CtrlInfo.Name).Call(nil)
|
||||
})
|
||||
}
|
||||
if !m.request.IsExited() {
|
||||
@ -86,17 +86,17 @@ func (m *middleware) Next() {
|
||||
if m.request.IsExited() {
|
||||
break
|
||||
}
|
||||
if item.handler.initFunc != nil {
|
||||
if item.Handler.InitFunc != nil {
|
||||
niceCallFunc(func() {
|
||||
item.handler.initFunc(m.request)
|
||||
item.Handler.InitFunc(m.request)
|
||||
})
|
||||
}
|
||||
if !m.request.IsExited() {
|
||||
m.callHandlerFunc(item.handler.itemInfo)
|
||||
m.callHandlerFunc(item.Handler.Info)
|
||||
}
|
||||
if !m.request.IsExited() && item.handler.shutFunc != nil {
|
||||
if !m.request.IsExited() && item.Handler.ShutFunc != nil {
|
||||
niceCallFunc(func() {
|
||||
item.handler.shutFunc(m.request)
|
||||
item.Handler.ShutFunc(m.request)
|
||||
})
|
||||
}
|
||||
|
||||
@ -107,13 +107,13 @@ func (m *middleware) Next() {
|
||||
break
|
||||
}
|
||||
niceCallFunc(func() {
|
||||
m.callHandlerFunc(item.handler.itemInfo)
|
||||
m.callHandlerFunc(item.Handler.Info)
|
||||
})
|
||||
|
||||
// Global middleware array.
|
||||
case handlerTypeMiddleware:
|
||||
niceCallFunc(func() {
|
||||
item.handler.itemInfo.Func(m.request)
|
||||
item.Handler.Info.Func(m.request)
|
||||
})
|
||||
// It does not continue calling next middleware after another middleware done.
|
||||
// There should be a "Next" function to be called in the middleware in order to manage the workflow.
|
||||
|
@ -221,7 +221,7 @@ func (s *Server) dumpRouterMap() {
|
||||
data[2] = item.Address
|
||||
data[3] = item.Method
|
||||
data[4] = item.Route
|
||||
data[5] = item.handler.itemName
|
||||
data[5] = item.handler.Name
|
||||
data[6] = item.Middleware
|
||||
table.Append(data)
|
||||
}
|
||||
@ -248,21 +248,21 @@ func (s *Server) GetRouterArray() []RouterItem {
|
||||
Server: s.name,
|
||||
Address: address,
|
||||
Domain: array[4],
|
||||
Type: registeredItem.handler.itemType,
|
||||
Type: registeredItem.Handler.Type,
|
||||
Middleware: array[1],
|
||||
Method: array[2],
|
||||
Route: array[3],
|
||||
Priority: len(registeredItems) - index - 1,
|
||||
handler: registeredItem.handler,
|
||||
handler: registeredItem.Handler,
|
||||
}
|
||||
switch item.handler.itemType {
|
||||
switch item.handler.Type {
|
||||
case handlerTypeController, handlerTypeObject, handlerTypeHandler:
|
||||
item.IsServiceHandler = true
|
||||
case handlerTypeMiddleware:
|
||||
item.Middleware = "GLOBAL MIDDLEWARE"
|
||||
}
|
||||
if len(item.handler.middleware) > 0 {
|
||||
for _, v := range item.handler.middleware {
|
||||
if len(item.handler.Middleware) > 0 {
|
||||
for _, v := range item.handler.Middleware {
|
||||
if item.Middleware != "" {
|
||||
item.Middleware += ","
|
||||
}
|
||||
@ -280,9 +280,9 @@ func (s *Server) GetRouterArray() []RouterItem {
|
||||
if r = strings.Compare(item1.Domain, item2.Domain); r == 0 {
|
||||
if r = strings.Compare(item1.Route, item2.Route); r == 0 {
|
||||
if r = strings.Compare(item1.Method, item2.Method); r == 0 {
|
||||
if item1.handler.itemType == handlerTypeMiddleware && item2.handler.itemType != handlerTypeMiddleware {
|
||||
if item1.handler.Type == handlerTypeMiddleware && item2.handler.Type != handlerTypeMiddleware {
|
||||
return -1
|
||||
} else if item1.handler.itemType == handlerTypeMiddleware && item2.handler.itemType == handlerTypeMiddleware {
|
||||
} else if item1.handler.Type == handlerTypeMiddleware && item2.handler.Type == handlerTypeMiddleware {
|
||||
return 1
|
||||
} else if r = strings.Compare(item1.Middleware, item2.Middleware); r == 0 {
|
||||
r = item2.Priority - item1.Priority
|
||||
|
@ -66,10 +66,10 @@ func (s *Server) parsePattern(pattern string) (domain, method, path string, err
|
||||
// This function is called during server starts up, which cares little about the performance. What really cares
|
||||
// is the well designed router storage structure for router searching when the request is under serving.
|
||||
func (s *Server) setHandler(pattern string, handler *handlerItem) {
|
||||
handler.itemId = handlerIdGenerator.Add(1)
|
||||
if handler.source == "" {
|
||||
handler.Id = handlerIdGenerator.Add(1)
|
||||
if handler.Source == "" {
|
||||
_, file, line := gdebug.CallerWithFilter(stackFilterKey)
|
||||
handler.source = fmt.Sprintf(`%s:%d`, file, line)
|
||||
handler.Source = fmt.Sprintf(`%s:%d`, file, line)
|
||||
}
|
||||
domain, method, uri, err := s.parsePattern(pattern)
|
||||
if err != nil {
|
||||
@ -82,27 +82,27 @@ func (s *Server) setHandler(pattern string, handler *handlerItem) {
|
||||
}
|
||||
|
||||
// Repeated router checks, this feature can be disabled by server configuration.
|
||||
routerKey := s.routerMapKey(handler.hookName, method, uri, domain)
|
||||
routerKey := s.routerMapKey(handler.HookName, method, uri, domain)
|
||||
if !s.config.RouteOverWrite {
|
||||
switch handler.itemType {
|
||||
switch handler.Type {
|
||||
case handlerTypeHandler, handlerTypeObject, handlerTypeController:
|
||||
if item, ok := s.routesMap[routerKey]; ok {
|
||||
s.Logger().Fatalf(
|
||||
`duplicated route registry "%s" at %s , already registered at %s`,
|
||||
pattern, handler.source, item[0].source,
|
||||
pattern, handler.Source, item[0].Source,
|
||||
)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
// Create a new router by given parameter.
|
||||
handler.router = &Router{
|
||||
handler.Router = &Router{
|
||||
Uri: uri,
|
||||
Domain: domain,
|
||||
Method: strings.ToUpper(method),
|
||||
Priority: strings.Count(uri[1:], "/"),
|
||||
}
|
||||
handler.router.RegRule, handler.router.RegNames = s.patternToRegular(uri)
|
||||
handler.Router.RegRule, handler.Router.RegNames = s.patternToRegular(uri)
|
||||
|
||||
if _, ok := s.serveTree[domain]; !ok {
|
||||
s.serveTree[domain] = make(map[string]interface{})
|
||||
@ -193,10 +193,10 @@ func (s *Server) setHandler(pattern string, handler *handlerItem) {
|
||||
}
|
||||
|
||||
routeItem := registeredRouteItem{
|
||||
source: handler.source,
|
||||
handler: handler,
|
||||
Source: handler.Source,
|
||||
Handler: handler,
|
||||
}
|
||||
switch handler.itemType {
|
||||
switch handler.Type {
|
||||
case handlerTypeHandler, handlerTypeObject, handlerTypeController:
|
||||
// Overwrite the route.
|
||||
s.routesMap[routerKey] = []registeredRouteItem{routeItem}
|
||||
@ -216,18 +216,18 @@ func (s *Server) setHandler(pattern string, handler *handlerItem) {
|
||||
// 3. Route type: {xxx} > :xxx > *xxx.
|
||||
func (s *Server) compareRouterPriority(newItem *handlerItem, oldItem *handlerItem) bool {
|
||||
// If they're all type of middleware, the priority is according their registered sequence.
|
||||
if newItem.itemType == handlerTypeMiddleware && oldItem.itemType == handlerTypeMiddleware {
|
||||
if newItem.Type == handlerTypeMiddleware && oldItem.Type == handlerTypeMiddleware {
|
||||
return false
|
||||
}
|
||||
// The middleware has the most high priority.
|
||||
if newItem.itemType == handlerTypeMiddleware && oldItem.itemType != handlerTypeMiddleware {
|
||||
if newItem.Type == handlerTypeMiddleware && oldItem.Type != handlerTypeMiddleware {
|
||||
return true
|
||||
}
|
||||
// URI: The deeper the higher (simply check the count of char '/' in the URI).
|
||||
if newItem.router.Priority > oldItem.router.Priority {
|
||||
if newItem.Router.Priority > oldItem.Router.Priority {
|
||||
return true
|
||||
}
|
||||
if newItem.router.Priority < oldItem.router.Priority {
|
||||
if newItem.Router.Priority < oldItem.Router.Priority {
|
||||
return false
|
||||
}
|
||||
|
||||
@ -238,8 +238,8 @@ func (s *Server) compareRouterPriority(newItem *handlerItem, oldItem *handlerIte
|
||||
// /admin-goods-{page} > /admin-{page}
|
||||
// /{hash}.{type} > /{hash}
|
||||
var uriNew, uriOld string
|
||||
uriNew, _ = gregex.ReplaceString(`\{[^/]+?\}`, "", newItem.router.Uri)
|
||||
uriOld, _ = gregex.ReplaceString(`\{[^/]+?\}`, "", oldItem.router.Uri)
|
||||
uriNew, _ = gregex.ReplaceString(`\{[^/]+?\}`, "", newItem.Router.Uri)
|
||||
uriOld, _ = gregex.ReplaceString(`\{[^/]+?\}`, "", oldItem.Router.Uri)
|
||||
uriNew, _ = gregex.ReplaceString(`:[^/]+?`, "", uriNew)
|
||||
uriOld, _ = gregex.ReplaceString(`:[^/]+?`, "", uriOld)
|
||||
uriNew, _ = gregex.ReplaceString(`\*[^/]*`, "", uriNew) // Replace "/*" and "/*any".
|
||||
@ -264,7 +264,7 @@ func (s *Server) compareRouterPriority(newItem *handlerItem, oldItem *handlerIte
|
||||
fuzzyCountTotalNew int
|
||||
fuzzyCountTotalOld int
|
||||
)
|
||||
for _, v := range newItem.router.Uri {
|
||||
for _, v := range newItem.Router.Uri {
|
||||
switch v {
|
||||
case '{':
|
||||
fuzzyCountFieldNew++
|
||||
@ -274,7 +274,7 @@ func (s *Server) compareRouterPriority(newItem *handlerItem, oldItem *handlerIte
|
||||
fuzzyCountAnyNew++
|
||||
}
|
||||
}
|
||||
for _, v := range oldItem.router.Uri {
|
||||
for _, v := range oldItem.Router.Uri {
|
||||
switch v {
|
||||
case '{':
|
||||
fuzzyCountFieldOld++
|
||||
@ -312,16 +312,16 @@ func (s *Server) compareRouterPriority(newItem *handlerItem, oldItem *handlerIte
|
||||
|
||||
// It then compares the accuracy of their http method,
|
||||
// the more accurate the more priority.
|
||||
if newItem.router.Method != defaultMethod {
|
||||
if newItem.Router.Method != defaultMethod {
|
||||
return true
|
||||
}
|
||||
if oldItem.router.Method != defaultMethod {
|
||||
if oldItem.Router.Method != defaultMethod {
|
||||
return true
|
||||
}
|
||||
|
||||
// If they have different router type,
|
||||
// the new router item has more priority than the other one.
|
||||
if newItem.itemType == handlerTypeHandler || newItem.itemType == handlerTypeObject || newItem.itemType == handlerTypeController {
|
||||
if newItem.Type == handlerTypeHandler || newItem.Type == handlerTypeObject || newItem.Type == handlerTypeController {
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -19,14 +19,14 @@ func (s *Server) BindHookHandler(pattern string, hook string, handler HandlerFun
|
||||
|
||||
func (s *Server) doBindHookHandler(pattern string, hook string, handler HandlerFunc, source string) {
|
||||
s.setHandler(pattern, &handlerItem{
|
||||
itemType: handlerTypeHook,
|
||||
itemName: gdebug.FuncPath(handler),
|
||||
itemInfo: handlerFuncInfo{
|
||||
Type: handlerTypeHook,
|
||||
Name: gdebug.FuncPath(handler),
|
||||
Info: handlerFuncInfo{
|
||||
Func: handler,
|
||||
Type: reflect.TypeOf(handler),
|
||||
},
|
||||
hookName: hook,
|
||||
source: source,
|
||||
HookName: hook,
|
||||
Source: source,
|
||||
})
|
||||
}
|
||||
|
||||
@ -43,11 +43,11 @@ func (s *Server) callHookHandler(hook string, r *Request) {
|
||||
// Backup the old router variable map.
|
||||
oldRouterMap := r.routerMap
|
||||
for _, item := range hookItems {
|
||||
r.routerMap = item.values
|
||||
r.routerMap = item.Values
|
||||
// DO NOT USE the router of the hook handler,
|
||||
// which can overwrite the router of serving handler.
|
||||
// r.Router = item.handler.router
|
||||
if err := s.niceCallHookHandler(item.handler.itemInfo.Func, r); err != nil {
|
||||
if err := s.niceCallHookHandler(item.Handler.Info.Func, r); err != nil {
|
||||
switch err {
|
||||
case exceptionExit:
|
||||
break
|
||||
@ -73,7 +73,7 @@ func (r *Request) getHookHandlers(hook string) []*handlerParsedItem {
|
||||
}
|
||||
parsedItems := make([]*handlerParsedItem, 0, 4)
|
||||
for _, v := range r.handlers {
|
||||
if v.handler.hookName != hook {
|
||||
if v.Handler.HookName != hook {
|
||||
continue
|
||||
}
|
||||
item := v
|
||||
|
@ -23,9 +23,9 @@ const (
|
||||
func (s *Server) BindMiddleware(pattern string, handlers ...HandlerFunc) {
|
||||
for _, handler := range handlers {
|
||||
s.setHandler(pattern, &handlerItem{
|
||||
itemType: handlerTypeMiddleware,
|
||||
itemName: gdebug.FuncPath(handler),
|
||||
itemInfo: handlerFuncInfo{
|
||||
Type: handlerTypeMiddleware,
|
||||
Name: gdebug.FuncPath(handler),
|
||||
Info: handlerFuncInfo{
|
||||
Func: handler,
|
||||
Type: reflect.TypeOf(handler),
|
||||
},
|
||||
@ -39,9 +39,9 @@ func (s *Server) BindMiddleware(pattern string, handlers ...HandlerFunc) {
|
||||
func (s *Server) BindMiddlewareDefault(handlers ...HandlerFunc) {
|
||||
for _, handler := range handlers {
|
||||
s.setHandler(defaultMiddlewarePattern, &handlerItem{
|
||||
itemType: handlerTypeMiddleware,
|
||||
itemName: gdebug.FuncPath(handler),
|
||||
itemInfo: handlerFuncInfo{
|
||||
Type: handlerTypeMiddleware,
|
||||
Name: gdebug.FuncPath(handler),
|
||||
Info: handlerFuncInfo{
|
||||
Func: handler,
|
||||
Type: reflect.TypeOf(handler),
|
||||
},
|
||||
|
@ -141,34 +141,34 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*han
|
||||
item := e.Value.(*handlerItem)
|
||||
// Filter repeated handler item, especially the middleware and hook handlers.
|
||||
// It is necessary, do not remove this checks logic unless you really know how it is necessary.
|
||||
if _, ok := repeatHandlerCheckMap[item.itemId]; ok {
|
||||
if _, ok := repeatHandlerCheckMap[item.Id]; ok {
|
||||
continue
|
||||
} else {
|
||||
repeatHandlerCheckMap[item.itemId] = struct{}{}
|
||||
repeatHandlerCheckMap[item.Id] = struct{}{}
|
||||
}
|
||||
// Serving handler can only be added to the handler array just once.
|
||||
if hasServe {
|
||||
switch item.itemType {
|
||||
switch item.Type {
|
||||
case handlerTypeHandler, handlerTypeObject, handlerTypeController:
|
||||
continue
|
||||
}
|
||||
}
|
||||
if item.router.Method == defaultMethod || item.router.Method == method {
|
||||
if item.Router.Method == defaultMethod || item.Router.Method == method {
|
||||
// Note the rule having no fuzzy rules: len(match) == 1
|
||||
if match, err := gregex.MatchString(item.router.RegRule, path); err == nil && len(match) > 0 {
|
||||
if match, err := gregex.MatchString(item.Router.RegRule, path); err == nil && len(match) > 0 {
|
||||
parsedItem := &handlerParsedItem{item, nil}
|
||||
// If the rule contains fuzzy names,
|
||||
// it needs paring the URL to retrieve the values for the names.
|
||||
if len(item.router.RegNames) > 0 {
|
||||
if len(match) > len(item.router.RegNames) {
|
||||
parsedItem.values = make(map[string]string)
|
||||
if len(item.Router.RegNames) > 0 {
|
||||
if len(match) > len(item.Router.RegNames) {
|
||||
parsedItem.Values = make(map[string]string)
|
||||
// It there repeated names, it just overwrites the same one.
|
||||
for i, name := range item.router.RegNames {
|
||||
parsedItem.values[name] = match[i+1]
|
||||
for i, name := range item.Router.RegNames {
|
||||
parsedItem.Values[name] = match[i+1]
|
||||
}
|
||||
}
|
||||
}
|
||||
switch item.itemType {
|
||||
switch item.Type {
|
||||
// The serving handler can be only added just once.
|
||||
case handlerTypeHandler, handlerTypeObject, handlerTypeController:
|
||||
hasServe = true
|
||||
@ -190,7 +190,7 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*han
|
||||
parsedItemList.PushBack(parsedItem)
|
||||
|
||||
default:
|
||||
panic(fmt.Sprintf(`invalid handler type %d`, item.itemType))
|
||||
panic(fmt.Sprintf(`invalid handler type %d`, item.Type))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -210,33 +210,33 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*han
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (item *handlerItem) MarshalJSON() ([]byte, error) {
|
||||
switch item.itemType {
|
||||
switch item.Type {
|
||||
case handlerTypeHook:
|
||||
return json.Marshal(
|
||||
fmt.Sprintf(
|
||||
`%s %s:%s (%s)`,
|
||||
item.router.Uri,
|
||||
item.router.Domain,
|
||||
item.router.Method,
|
||||
item.hookName,
|
||||
item.Router.Uri,
|
||||
item.Router.Domain,
|
||||
item.Router.Method,
|
||||
item.HookName,
|
||||
),
|
||||
)
|
||||
case handlerTypeMiddleware:
|
||||
return json.Marshal(
|
||||
fmt.Sprintf(
|
||||
`%s %s:%s (MIDDLEWARE)`,
|
||||
item.router.Uri,
|
||||
item.router.Domain,
|
||||
item.router.Method,
|
||||
item.Router.Uri,
|
||||
item.Router.Domain,
|
||||
item.Router.Method,
|
||||
),
|
||||
)
|
||||
default:
|
||||
return json.Marshal(
|
||||
fmt.Sprintf(
|
||||
`%s %s:%s`,
|
||||
item.router.Uri,
|
||||
item.router.Domain,
|
||||
item.router.Method,
|
||||
item.Router.Uri,
|
||||
item.Router.Domain,
|
||||
item.Router.Method,
|
||||
),
|
||||
)
|
||||
}
|
||||
@ -244,5 +244,5 @@ func (item *handlerItem) MarshalJSON() ([]byte, error) {
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (item *handlerParsedItem) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(item.handler)
|
||||
return json.Marshal(item.Handler)
|
||||
}
|
||||
|
@ -111,14 +111,14 @@ func (s *Server) doBindController(
|
||||
}
|
||||
key := s.mergeBuildInNameToPattern(pattern, structName, methodName, true)
|
||||
m[key] = &handlerItem{
|
||||
itemName: fmt.Sprintf(`%s.%s.%s`, pkgPath, ctlName, methodName),
|
||||
itemType: handlerTypeController,
|
||||
ctrlInfo: &handlerController{
|
||||
name: methodName,
|
||||
reflect: v.Elem().Type(),
|
||||
Name: fmt.Sprintf(`%s.%s.%s`, pkgPath, ctlName, methodName),
|
||||
Type: handlerTypeController,
|
||||
CtrlInfo: &handlerController{
|
||||
Name: methodName,
|
||||
Type: v.Elem().Type(),
|
||||
},
|
||||
middleware: middleware,
|
||||
source: source,
|
||||
Middleware: middleware,
|
||||
Source: source,
|
||||
}
|
||||
// If there's "Index" method, then an additional route is automatically added
|
||||
// to match the main URI, for example:
|
||||
@ -134,14 +134,14 @@ func (s *Server) doBindController(
|
||||
k = "/" + k
|
||||
}
|
||||
m[k] = &handlerItem{
|
||||
itemName: fmt.Sprintf(`%s.%s.%s`, pkgPath, ctlName, methodName),
|
||||
itemType: handlerTypeController,
|
||||
ctrlInfo: &handlerController{
|
||||
name: methodName,
|
||||
reflect: v.Elem().Type(),
|
||||
Name: fmt.Sprintf(`%s.%s.%s`, pkgPath, ctlName, methodName),
|
||||
Type: handlerTypeController,
|
||||
CtrlInfo: &handlerController{
|
||||
Name: methodName,
|
||||
Type: v.Elem().Type(),
|
||||
},
|
||||
middleware: middleware,
|
||||
source: source,
|
||||
Middleware: middleware,
|
||||
Source: source,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -182,14 +182,14 @@ func (s *Server) doBindControllerMethod(
|
||||
}
|
||||
key := s.mergeBuildInNameToPattern(pattern, structName, methodName, false)
|
||||
m[key] = &handlerItem{
|
||||
itemName: fmt.Sprintf(`%s.%s.%s`, pkgPath, ctlName, methodName),
|
||||
itemType: handlerTypeController,
|
||||
ctrlInfo: &handlerController{
|
||||
name: methodName,
|
||||
reflect: v.Elem().Type(),
|
||||
Name: fmt.Sprintf(`%s.%s.%s`, pkgPath, ctlName, methodName),
|
||||
Type: handlerTypeController,
|
||||
CtrlInfo: &handlerController{
|
||||
Name: methodName,
|
||||
Type: v.Elem().Type(),
|
||||
},
|
||||
middleware: middleware,
|
||||
source: source,
|
||||
Middleware: middleware,
|
||||
Source: source,
|
||||
}
|
||||
s.bindHandlerByMap(m)
|
||||
}
|
||||
@ -224,14 +224,14 @@ func (s *Server) doBindControllerRest(
|
||||
}
|
||||
key := s.mergeBuildInNameToPattern(methodName+":"+pattern, structName, methodName, false)
|
||||
m[key] = &handlerItem{
|
||||
itemName: fmt.Sprintf(`%s.%s.%s`, pkgPath, ctlName, methodName),
|
||||
itemType: handlerTypeController,
|
||||
ctrlInfo: &handlerController{
|
||||
name: methodName,
|
||||
reflect: v.Elem().Type(),
|
||||
Name: fmt.Sprintf(`%s.%s.%s`, pkgPath, ctlName, methodName),
|
||||
Type: handlerTypeController,
|
||||
CtrlInfo: &handlerController{
|
||||
Name: methodName,
|
||||
Type: v.Elem().Type(),
|
||||
},
|
||||
middleware: middleware,
|
||||
source: source,
|
||||
Middleware: middleware,
|
||||
Source: source,
|
||||
}
|
||||
}
|
||||
s.bindHandlerByMap(m)
|
||||
|
@ -37,11 +37,11 @@ func (s *Server) BindHandler(pattern string, handler interface{}) {
|
||||
// /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{
|
||||
itemName: gdebug.FuncPath(funcInfo.Func),
|
||||
itemType: handlerTypeHandler,
|
||||
itemInfo: funcInfo,
|
||||
middleware: middleware,
|
||||
source: source,
|
||||
Name: gdebug.FuncPath(funcInfo.Func),
|
||||
Type: handlerTypeHandler,
|
||||
Info: funcInfo,
|
||||
Middleware: middleware,
|
||||
Source: source,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -110,13 +110,13 @@ func (s *Server) doBindObject(pattern string, object interface{}, method string,
|
||||
|
||||
key := s.mergeBuildInNameToPattern(pattern, structName, methodName, true)
|
||||
m[key] = &handlerItem{
|
||||
itemName: fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, methodName),
|
||||
itemType: handlerTypeObject,
|
||||
itemInfo: funcInfo,
|
||||
initFunc: initFunc,
|
||||
shutFunc: shutFunc,
|
||||
middleware: middleware,
|
||||
source: source,
|
||||
Name: fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, methodName),
|
||||
Type: handlerTypeObject,
|
||||
Info: funcInfo,
|
||||
InitFunc: initFunc,
|
||||
ShutFunc: shutFunc,
|
||||
Middleware: middleware,
|
||||
Source: source,
|
||||
}
|
||||
// If there's "Index" method, then an additional route is automatically added
|
||||
// to match the main URI, for example:
|
||||
@ -132,13 +132,13 @@ func (s *Server) doBindObject(pattern string, object interface{}, method string,
|
||||
k = "/" + k
|
||||
}
|
||||
m[k] = &handlerItem{
|
||||
itemName: fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, methodName),
|
||||
itemType: handlerTypeObject,
|
||||
itemInfo: funcInfo,
|
||||
initFunc: initFunc,
|
||||
shutFunc: shutFunc,
|
||||
middleware: middleware,
|
||||
source: source,
|
||||
Name: fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, methodName),
|
||||
Type: handlerTypeObject,
|
||||
Info: funcInfo,
|
||||
InitFunc: initFunc,
|
||||
ShutFunc: shutFunc,
|
||||
Middleware: middleware,
|
||||
Source: source,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -192,13 +192,13 @@ func (s *Server) doBindObjectMethod(
|
||||
|
||||
key := s.mergeBuildInNameToPattern(pattern, structName, methodName, false)
|
||||
m[key] = &handlerItem{
|
||||
itemName: fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, methodName),
|
||||
itemType: handlerTypeObject,
|
||||
itemInfo: funcInfo,
|
||||
initFunc: initFunc,
|
||||
shutFunc: shutFunc,
|
||||
middleware: middleware,
|
||||
source: source,
|
||||
Name: fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, methodName),
|
||||
Type: handlerTypeObject,
|
||||
Info: funcInfo,
|
||||
InitFunc: initFunc,
|
||||
ShutFunc: shutFunc,
|
||||
Middleware: middleware,
|
||||
Source: source,
|
||||
}
|
||||
|
||||
s.bindHandlerByMap(m)
|
||||
@ -247,13 +247,13 @@ func (s *Server) doBindObjectRest(pattern string, object interface{}, middleware
|
||||
|
||||
key := s.mergeBuildInNameToPattern(methodName+":"+pattern, structName, methodName, false)
|
||||
m[key] = &handlerItem{
|
||||
itemName: fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, methodName),
|
||||
itemType: handlerTypeObject,
|
||||
itemInfo: funcInfo,
|
||||
initFunc: initFunc,
|
||||
shutFunc: shutFunc,
|
||||
middleware: middleware,
|
||||
source: source,
|
||||
Name: fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, methodName),
|
||||
Type: handlerTypeObject,
|
||||
Info: funcInfo,
|
||||
InitFunc: initFunc,
|
||||
ShutFunc: shutFunc,
|
||||
Middleware: middleware,
|
||||
Source: source,
|
||||
}
|
||||
}
|
||||
s.bindHandlerByMap(m)
|
||||
|
@ -76,7 +76,7 @@ func Test_Router_Handler_Extended_Handler_WithObject(t *testing.T) {
|
||||
client := g.Client()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
t.Assert(client.GetContent("/test?age=18&name=john"), `{"Id":1,"Age":18,"Name":"john"}`)
|
||||
t.Assert(client.GetContent("/test/error"), `{"code":-1,"message":"error"}`)
|
||||
t.Assert(client.GetContent("/test?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Age":18,"Name":"john"}}`)
|
||||
t.Assert(client.GetContent("/test/error"), `{"code":-1,"message":"error","data":null}`)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user