Add Group

This commit is contained in:
Nise Void 2018-08-19 17:03:53 +02:00
parent f4d27689e7
commit 78000acf6c
Signed by untrusted user: NiseVoid
GPG Key ID: FBA14AC83EA602F3
2 changed files with 53 additions and 0 deletions

49
group.go Normal file
View 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)
}

View File

@ -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})