mirror of
https://gitee.com/johng/gf.git
synced 2024-12-01 11:48:09 +08:00
improve ghttp.RouterGroup
This commit is contained in:
parent
573179060e
commit
846c6a579e
@ -55,7 +55,7 @@ func (s *Server) handlePreBindItems() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 获取分组路由对象
|
// 获取分组路由对象
|
||||||
func (s *Server) Group(prefix string, groups ...*RouterGroup) *RouterGroup {
|
func (s *Server) Group(prefix string, groups ...func(g *RouterGroup)) *RouterGroup {
|
||||||
if prefix == "/" {
|
if prefix == "/" {
|
||||||
prefix = ""
|
prefix = ""
|
||||||
}
|
}
|
||||||
@ -65,14 +65,14 @@ func (s *Server) Group(prefix string, groups ...*RouterGroup) *RouterGroup {
|
|||||||
}
|
}
|
||||||
if len(groups) > 0 {
|
if len(groups) > 0 {
|
||||||
for _, v := range groups {
|
for _, v := range groups {
|
||||||
v.parent = group
|
v(group)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return group
|
return group
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取分组路由对象(绑定域名)
|
// 获取分组路由对象(绑定域名)
|
||||||
func (d *Domain) Group(prefix string, groups ...*RouterGroup) *RouterGroup {
|
func (d *Domain) Group(prefix string, groups ...func(g *RouterGroup)) *RouterGroup {
|
||||||
if prefix == "/" {
|
if prefix == "/" {
|
||||||
prefix = ""
|
prefix = ""
|
||||||
}
|
}
|
||||||
@ -82,14 +82,14 @@ func (d *Domain) Group(prefix string, groups ...*RouterGroup) *RouterGroup {
|
|||||||
}
|
}
|
||||||
if len(groups) > 0 {
|
if len(groups) > 0 {
|
||||||
for _, v := range groups {
|
for _, v := range groups {
|
||||||
v.parent = group
|
v(group)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return group
|
return group
|
||||||
}
|
}
|
||||||
|
|
||||||
// 层级递归创建分组路由注册项
|
// 层级递归创建分组路由注册项
|
||||||
func (g *RouterGroup) Group(prefix string, groups ...*RouterGroup) *RouterGroup {
|
func (g *RouterGroup) Group(prefix string, groups ...func(g *RouterGroup)) *RouterGroup {
|
||||||
if prefix == "/" {
|
if prefix == "/" {
|
||||||
prefix = ""
|
prefix = ""
|
||||||
}
|
}
|
||||||
@ -97,11 +97,11 @@ func (g *RouterGroup) Group(prefix string, groups ...*RouterGroup) *RouterGroup
|
|||||||
parent: g,
|
parent: g,
|
||||||
server: g.server,
|
server: g.server,
|
||||||
domain: g.domain,
|
domain: g.domain,
|
||||||
prefix: g.prefix + prefix,
|
prefix: prefix,
|
||||||
}
|
}
|
||||||
if len(groups) > 0 {
|
if len(groups) > 0 {
|
||||||
for _, v := range groups {
|
for _, v := range groups {
|
||||||
v.parent = group
|
v(group)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return group
|
return group
|
||||||
|
@ -19,46 +19,45 @@ import (
|
|||||||
func Test_Router_Group_Group(t *testing.T) {
|
func Test_Router_Group_Group(t *testing.T) {
|
||||||
p := ports.PopRand()
|
p := ports.PopRand()
|
||||||
s := g.Server(p)
|
s := g.Server(p)
|
||||||
g := s.Group("/")
|
s.Group("/api.v2", func(g *ghttp.RouterGroup) {
|
||||||
g.Group("/api.v2",
|
|
||||||
g.Middleware(func(r *ghttp.Request) {
|
g.Middleware(func(r *ghttp.Request) {
|
||||||
r.Response.Write("1")
|
r.Response.Write("1")
|
||||||
r.Middleware.Next()
|
r.Middleware.Next()
|
||||||
r.Response.Write("2")
|
r.Response.Write("2")
|
||||||
}),
|
})
|
||||||
g.GET("/test", func(r *ghttp.Request) {
|
g.GET("/test", func(r *ghttp.Request) {
|
||||||
r.Response.Write("test")
|
r.Response.Write("test")
|
||||||
}),
|
})
|
||||||
g.Group("/order",
|
g.Group("/order", func(g *ghttp.RouterGroup) {
|
||||||
g.GET("/list", func(r *ghttp.Request) {
|
g.GET("/list", func(r *ghttp.Request) {
|
||||||
r.Response.Write("list")
|
r.Response.Write("list")
|
||||||
}),
|
})
|
||||||
g.PUT("/update", func(r *ghttp.Request) {
|
g.PUT("/update", func(r *ghttp.Request) {
|
||||||
r.Response.Write("update")
|
r.Response.Write("update")
|
||||||
}),
|
})
|
||||||
),
|
})
|
||||||
g.Group("/user",
|
g.Group("/user", func(g *ghttp.RouterGroup) {
|
||||||
g.GET("/info", func(r *ghttp.Request) {
|
g.GET("/info", func(r *ghttp.Request) {
|
||||||
r.Response.Write("info")
|
r.Response.Write("info")
|
||||||
}),
|
})
|
||||||
g.POST("/edit", func(r *ghttp.Request) {
|
g.POST("/edit", func(r *ghttp.Request) {
|
||||||
r.Response.Write("edit")
|
r.Response.Write("edit")
|
||||||
}),
|
})
|
||||||
g.DELETE("/drop", func(r *ghttp.Request) {
|
g.DELETE("/drop", func(r *ghttp.Request) {
|
||||||
r.Response.Write("drop")
|
r.Response.Write("drop")
|
||||||
}),
|
})
|
||||||
),
|
})
|
||||||
g.Group("/hook",
|
g.Group("/hook", func(g *ghttp.RouterGroup) {
|
||||||
g.Hook("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
g.Hook("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||||
r.Response.Write("hook any")
|
r.Response.Write("hook any")
|
||||||
}),
|
})
|
||||||
g.Hook("/:name", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
g.Hook("/:name", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||||
r.Response.Write("hook name")
|
r.Response.Write("hook name")
|
||||||
}),
|
})
|
||||||
),
|
})
|
||||||
)
|
})
|
||||||
s.SetPort(p)
|
s.SetPort(p)
|
||||||
//s.SetDumpRouteMap(false)
|
s.SetDumpRouteMap(false)
|
||||||
s.Start()
|
s.Start()
|
||||||
defer s.Shutdown()
|
defer s.Shutdown()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user