mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
fix issue of router group in auto-adding 'index' router for controller and object
This commit is contained in:
@ -64,15 +64,11 @@ func (s *Server)BindController(pattern string, c Controller, methods...string) e
|
||||
faddr : nil,
|
||||
}
|
||||
// 如果方法中带有Index方法,那么额外自动增加一个路由规则匹配主URI
|
||||
// 例如: pattern为/user, 那么会同时注册/user及/user/index,
|
||||
// 这里处理新增/user路由绑定
|
||||
if strings.EqualFold(mname, "Index") {
|
||||
p := key
|
||||
if strings.EqualFold(p[len(p) - 6:], "/index") {
|
||||
p = p[0 : len(p) - 6]
|
||||
if len(p) == 0 {
|
||||
p = "/"
|
||||
}
|
||||
}
|
||||
m[p] = &handlerItem {
|
||||
p := gstr.PosR(key, "/index")
|
||||
m[key[0 : p] + key[p + 6 : ]] = &handlerItem {
|
||||
name : fmt.Sprintf(`%s.%s.%s`, pkgPath, ctlName, mname),
|
||||
rtype : gROUTE_REGISTER_CONTROLLER,
|
||||
ctype : v.Elem().Type(),
|
||||
|
||||
@ -74,14 +74,8 @@ func (s *Server)BindObject(pattern string, obj interface{}, methods...string) er
|
||||
}
|
||||
// 如果方法中带有Index方法,那么额外自动增加一个路由规则匹配主URI
|
||||
if strings.EqualFold(mname, "Index") {
|
||||
p := key
|
||||
if strings.EqualFold(p[len(p) - 6:], "/index") {
|
||||
p = p[0 : len(p) - 6]
|
||||
if len(p) == 0 {
|
||||
p = "/"
|
||||
}
|
||||
}
|
||||
m[p] = &handlerItem {
|
||||
p := gstr.PosR(key, "/index")
|
||||
m[key[0 : p] + key[p + 6 : ]] = &handlerItem {
|
||||
name : fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, mname),
|
||||
rtype : gROUTE_REGISTER_OBJECT,
|
||||
ctype : nil,
|
||||
|
||||
@ -20,6 +20,10 @@ import (
|
||||
// 执行对象
|
||||
type Object struct {}
|
||||
|
||||
func (o *Object) Index(r *ghttp.Request) {
|
||||
r.Response.Write("Object Index")
|
||||
}
|
||||
|
||||
func (o *Object) Show(r *ghttp.Request) {
|
||||
r.Response.Write("Object Show")
|
||||
}
|
||||
@ -33,6 +37,10 @@ type Controller struct {
|
||||
gmvc.Controller
|
||||
}
|
||||
|
||||
func (c *Controller) Index() {
|
||||
c.Response.Write("Controller Index")
|
||||
}
|
||||
|
||||
func (c *Controller) Show() {
|
||||
c.Response.Write("Controller Show")
|
||||
}
|
||||
@ -72,11 +80,17 @@ func Test_Router_Group1(t *testing.T) {
|
||||
|
||||
gtest.Assert(client.GetContent ("/api/handler"), "Handler")
|
||||
|
||||
gtest.Assert(client.GetContent ("/api/ctl"), "Controller Index")
|
||||
gtest.Assert(client.GetContent ("/api/ctl/"), "Controller Index")
|
||||
gtest.Assert(client.GetContent ("/api/ctl/index"), "Controller Index")
|
||||
gtest.Assert(client.GetContent ("/api/ctl/my-show"), "Controller Show")
|
||||
gtest.Assert(client.GetContent ("/api/ctl/post"), "Controller REST Post")
|
||||
gtest.Assert(client.GetContent ("/api/ctl/show"), "Controller Show")
|
||||
gtest.Assert(client.PostContent("/api/ctl/rest"), "Controller REST Post")
|
||||
|
||||
gtest.Assert(client.GetContent ("/api/obj"), "Object Index")
|
||||
gtest.Assert(client.GetContent ("/api/obj/"), "Object Index")
|
||||
gtest.Assert(client.GetContent ("/api/obj/index"), "Object Index")
|
||||
gtest.Assert(client.GetContent ("/api/obj/delete"), "Object REST Delete")
|
||||
gtest.Assert(client.GetContent ("/api/obj/my-show"), "Object Show")
|
||||
gtest.Assert(client.GetContent ("/api/obj/show"), "Object Show")
|
||||
@ -130,7 +130,9 @@ func (p *Pool) File() (*File, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if p.inited.Set(true) == false {
|
||||
// !p.inited.Val() 使用原子读取操作判断,保证该操作判断的效率;
|
||||
// p.inited.Set(true) == false 使用原子写入操作,保证该操作的原子性;
|
||||
if !p.inited.Val() && p.inited.Set(true) == false {
|
||||
gfsnotify.Add(f.path, func(event *gfsnotify.Event) {
|
||||
// 如果文件被删除或者重命名,立即重建指针池
|
||||
if event.IsRemove() || event.IsRename() {
|
||||
|
||||
Reference in New Issue
Block a user