forked from Fuyu/router
Add Group
This commit is contained in:
parent
f4d27689e7
commit
78000acf6c
49
group.go
Normal file
49
group.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package router
|
||||||
|
|
||||||
|
import urlpath "path"
|
||||||
|
|
||||||
|
type Group struct {
|
||||||
|
router *Router
|
||||||
|
prefix string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Group) Group(prefix string) *Group {
|
||||||
|
return &Group{prefix: urlpath.Join(g.prefix, prefix), router: g.router}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET adds a GET route
|
||||||
|
func (g *Group) GET(path string, handle GetHandle) {
|
||||||
|
g.router.GET(urlpath.Join(g.prefix, path), handle)
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST adds a POST route
|
||||||
|
func (g *Group) POST(path string, handle interface{}) {
|
||||||
|
g.router.POST(urlpath.Join(g.prefix, path), handle)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DELETE adds a DELETE route
|
||||||
|
func (g *Group) DELETE(path string, handle GetHandle) {
|
||||||
|
g.router.DELETE(urlpath.Join(g.prefix, path), handle)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PUT adds a PUT route
|
||||||
|
func (g *Group) PUT(path string, handle interface{}) {
|
||||||
|
checkInterfaceHandle(handle)
|
||||||
|
g.router.PUT(urlpath.Join(g.prefix, path), handle)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PATCH adds a PATCH route
|
||||||
|
func (g *Group) PATCH(path string, handle interface{}) {
|
||||||
|
checkInterfaceHandle(handle)
|
||||||
|
g.router.PATCH(urlpath.Join(g.prefix, path), handle)
|
||||||
|
}
|
||||||
|
|
||||||
|
// HEAD adds a HEAD route
|
||||||
|
func (g *Group) HEAD(path string, handle GetHandle) {
|
||||||
|
g.router.HEAD(urlpath.Join(g.prefix, path), handle)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OPTIONS adds a OPTIONS route
|
||||||
|
func (g *Group) OPTIONS(path string, handle GetHandle) {
|
||||||
|
g.router.OPTIONS(urlpath.Join(g.prefix, path), handle)
|
||||||
|
}
|
@ -29,6 +29,10 @@ func New() *Router {
|
|||||||
return &Router{}
|
return &Router{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Router) Group(prefix string) *Group {
|
||||||
|
return &Group{prefix: prefix, router: r}
|
||||||
|
}
|
||||||
|
|
||||||
// GET adds a GET route
|
// GET adds a GET route
|
||||||
func (r *Router) GET(path string, handle GetHandle) {
|
func (r *Router) GET(path string, handle GetHandle) {
|
||||||
r.routes = append(r.routes, route{`GET`, path, handle})
|
r.routes = append(r.routes, route{`GET`, path, handle})
|
||||||
|
Loading…
Reference in New Issue
Block a user