router/group.go

54 lines
1.2 KiB
Go
Raw Normal View History

2018-08-19 17:03:53 +02:00
package router
import urlpath "path"
2018-08-26 11:17:05 +02:00
func join(prefix, path string) string {
return urlpath.Join(prefix, urlpath.Clean(path))
}
2018-08-19 17:03:53 +02:00
type Group struct {
router *Router
prefix string
}
func (g *Group) Group(prefix string) *Group {
2018-08-26 11:17:05 +02:00
return &Group{prefix: join(g.prefix, prefix), router: g.router}
2018-08-19 17:03:53 +02:00
}
// GET adds a GET route
func (g *Group) GET(path string, handle GetHandle) {
2018-08-26 11:17:05 +02:00
g.router.GET(join(g.prefix, path), handle)
2018-08-19 17:03:53 +02:00
}
// POST adds a POST route
func (g *Group) POST(path string, handle interface{}) {
2018-08-26 11:17:05 +02:00
g.router.POST(join(g.prefix, path), handle)
2018-08-19 17:03:53 +02:00
}
// DELETE adds a DELETE route
func (g *Group) DELETE(path string, handle GetHandle) {
2018-08-26 11:17:05 +02:00
g.router.DELETE(join(g.prefix, path), handle)
2018-08-19 17:03:53 +02:00
}
// PUT adds a PUT route
func (g *Group) PUT(path string, handle interface{}) {
checkInterfaceHandle(handle)
2018-08-26 11:17:05 +02:00
g.router.PUT(join(g.prefix, path), handle)
2018-08-19 17:03:53 +02:00
}
// PATCH adds a PATCH route
func (g *Group) PATCH(path string, handle interface{}) {
checkInterfaceHandle(handle)
2018-08-26 11:17:05 +02:00
g.router.PATCH(join(g.prefix, path), handle)
2018-08-19 17:03:53 +02:00
}
// HEAD adds a HEAD route
func (g *Group) HEAD(path string, handle GetHandle) {
2018-08-26 11:17:05 +02:00
g.router.HEAD(join(g.prefix, path), handle)
2018-08-19 17:03:53 +02:00
}
// OPTIONS adds a OPTIONS route
func (g *Group) OPTIONS(path string, handle GetHandle) {
2018-08-26 11:17:05 +02:00
g.router.OPTIONS(join(g.prefix, path), handle)
2018-08-19 17:03:53 +02:00
}