diff --git a/g/net/ghttp/ghttp_server_service_controller.go b/g/net/ghttp/ghttp_server_service_controller.go index 27b6bef94..b90ea08fa 100644 --- a/g/net/ghttp/ghttp_server_service_controller.go +++ b/g/net/ghttp/ghttp_server_service_controller.go @@ -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(), diff --git a/g/net/ghttp/ghttp_server_service_object.go b/g/net/ghttp/ghttp_server_service_object.go index f6a43bf8d..fb501c7b9 100644 --- a/g/net/ghttp/ghttp_server_service_object.go +++ b/g/net/ghttp/ghttp_server_service_object.go @@ -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, diff --git a/g/net/ghttp/ghttp_unit_3_test.go b/g/net/ghttp/ghttp_unit_param_test.go similarity index 100% rename from g/net/ghttp/ghttp_unit_3_test.go rename to g/net/ghttp/ghttp_unit_param_test.go diff --git a/g/net/ghttp/ghttp_unit_1_test.go b/g/net/ghttp/ghttp_unit_router_basic_test.go similarity index 100% rename from g/net/ghttp/ghttp_unit_1_test.go rename to g/net/ghttp/ghttp_unit_router_basic_test.go diff --git a/g/net/ghttp/ghttp_unit_2_test.go b/g/net/ghttp/ghttp_unit_router_group_test.go similarity index 87% rename from g/net/ghttp/ghttp_unit_2_test.go rename to g/net/ghttp/ghttp_unit_router_group_test.go index e5dfb7bde..83c53f972 100644 --- a/g/net/ghttp/ghttp_unit_2_test.go +++ b/g/net/ghttp/ghttp_unit_router_group_test.go @@ -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") diff --git a/g/os/gfpool/gfpool.go b/g/os/gfpool/gfpool.go index da8c29d09..00730c0e1 100644 --- a/g/os/gfpool/gfpool.go +++ b/g/os/gfpool/gfpool.go @@ -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() {