mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
improve router registry for ghttp
This commit is contained in:
@ -8,10 +8,11 @@
|
||||
package ghttp
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
)
|
||||
|
||||
// 分组路由对象
|
||||
@ -126,10 +127,10 @@ func (g *RouterGroup) bind(bindType string, pattern string, object interface{},
|
||||
if err != nil {
|
||||
glog.Fatalf("invalid pattern: %s", pattern)
|
||||
}
|
||||
if bindType == "HANDLER" {
|
||||
pattern = g.server.serveHandlerKey(method, g.prefix+"/"+strings.TrimLeft(path, "/"), domain)
|
||||
} else {
|
||||
if bindType == "REST" {
|
||||
pattern = g.prefix + "/" + strings.TrimLeft(path, "/")
|
||||
} else {
|
||||
pattern = g.server.serveHandlerKey(method, g.prefix+"/"+strings.TrimLeft(path, "/"), domain)
|
||||
}
|
||||
}
|
||||
methods := gconv.Strings(params)
|
||||
|
||||
@ -9,8 +9,9 @@ package ghttp
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"github.com/gogf/gf/g/text/gregex"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/g/text/gregex"
|
||||
)
|
||||
|
||||
// 查询请求处理方法.
|
||||
@ -117,5 +118,8 @@ func (s *Server) searchServeHandler(method, path, domain string) *handlerParsedI
|
||||
|
||||
// 生成回调方法查询的Key
|
||||
func (s *Server) serveHandlerKey(method, path, domain string) string {
|
||||
if method == "" {
|
||||
return path + "@" + strings.ToLower(domain)
|
||||
}
|
||||
return strings.ToUpper(method) + ":" + path + "@" + strings.ToLower(domain)
|
||||
}
|
||||
|
||||
@ -9,18 +9,29 @@ package ghttp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
"github.com/gogf/gf/g/text/gregex"
|
||||
"github.com/gogf/gf/g/text/gstr"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 绑定控制器,控制器需要实现 gmvc.Controller 接口,
|
||||
// 这种方式绑定的控制器每一次请求都会初始化一个新的控制器对象进行处理,对应不同的请求会话,
|
||||
// 第三个参数methods用以指定需要注册的方法,支持多个方法名称,多个方法以英文“,”号分隔,区分大小写.
|
||||
func (s *Server) BindController(pattern string, c Controller, methods ...string) {
|
||||
// 当pattern中的method为all时,去掉该method,以便于后续方法判断
|
||||
domain, method, path, err := s.parsePattern(pattern)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return
|
||||
}
|
||||
if strings.EqualFold(method, gDEFAULT_METHOD) {
|
||||
pattern = s.serveHandlerKey("", path, domain)
|
||||
}
|
||||
|
||||
methodMap := (map[string]bool)(nil)
|
||||
if len(methods) > 0 {
|
||||
methodMap = make(map[string]bool)
|
||||
|
||||
@ -9,17 +9,28 @@ package ghttp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
"github.com/gogf/gf/g/text/gregex"
|
||||
"github.com/gogf/gf/g/text/gstr"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 绑定对象到URI请求处理中,会自动识别方法名称,并附加到对应的URI地址后面
|
||||
// 第三个参数methods用以指定需要注册的方法,支持多个方法名称,多个方法以英文“,”号分隔,区分大小写
|
||||
func (s *Server) BindObject(pattern string, obj interface{}, methods ...string) {
|
||||
// 当pattern中的method为all时,去掉该method,以便于后续方法判断
|
||||
domain, method, path, err := s.parsePattern(pattern)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return
|
||||
}
|
||||
if strings.EqualFold(method, gDEFAULT_METHOD) {
|
||||
pattern = s.serveHandlerKey("", path, domain)
|
||||
}
|
||||
|
||||
methodMap := (map[string]bool)(nil)
|
||||
if len(methods) > 0 {
|
||||
methodMap = make(map[string]bool)
|
||||
|
||||
Reference in New Issue
Block a user