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-10-09 18:07:43 +02:00
|
|
|
// Group is a router group with a shared prefix and set of middlewares
|
2018-08-19 17:03:53 +02:00
|
|
|
type Group struct {
|
2018-10-09 18:07:43 +02:00
|
|
|
router *Router
|
|
|
|
prefix string
|
|
|
|
middleware []Middleware
|
2018-08-19 17:03:53 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 18:07:43 +02:00
|
|
|
// Group creates a new router group with a shared prefix and set of middlewares
|
|
|
|
func (g *Group) Group(prefix string, middleware ...Middleware) *Group {
|
|
|
|
return &Group{prefix: join(g.prefix, prefix), router: g.router, middleware: append(g.middleware, middleware...)}
|
2018-08-19 17:03:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GET adds a GET route
|
2018-10-09 18:07:43 +02:00
|
|
|
func (g *Group) GET(path string, handle Handle, middleware ...Middleware) {
|
|
|
|
g.router.GET(join(g.prefix, path), handle, append(g.middleware, middleware...)...)
|
2018-08-19 17:03:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// POST adds a POST route
|
2018-10-09 18:07:43 +02:00
|
|
|
func (g *Group) POST(path string, handle interface{}, middleware ...Middleware) {
|
|
|
|
g.router.POST(join(g.prefix, path), handle, append(g.middleware, middleware...)...)
|
2018-08-19 17:03:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE adds a DELETE route
|
2018-10-09 18:07:43 +02:00
|
|
|
func (g *Group) DELETE(path string, handle Handle, middleware ...Middleware) {
|
|
|
|
g.router.DELETE(join(g.prefix, path), handle, append(g.middleware, middleware...)...)
|
2018-08-19 17:03:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PUT adds a PUT route
|
2018-10-09 18:07:43 +02:00
|
|
|
func (g *Group) PUT(path string, handle interface{}, middleware ...Middleware) {
|
|
|
|
g.router.PUT(join(g.prefix, path), handle, append(g.middleware, middleware...)...)
|
2018-08-19 17:03:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PATCH adds a PATCH route
|
2018-10-09 18:07:43 +02:00
|
|
|
func (g *Group) PATCH(path string, handle interface{}, middleware ...Middleware) {
|
|
|
|
g.router.PATCH(join(g.prefix, path), handle, append(g.middleware, middleware...)...)
|
2018-08-19 17:03:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// HEAD adds a HEAD route
|
2018-10-09 18:07:43 +02:00
|
|
|
func (g *Group) HEAD(path string, handle Handle, middleware ...Middleware) {
|
|
|
|
g.router.HEAD(join(g.prefix, path), handle, append(g.middleware, middleware...)...)
|
2018-08-19 17:03:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// OPTIONS adds a OPTIONS route
|
2018-10-09 18:07:43 +02:00
|
|
|
func (g *Group) OPTIONS(path string, handle Handle, middleware ...Middleware) {
|
|
|
|
g.router.OPTIONS(join(g.prefix, path), handle, append(g.middleware, middleware...)...)
|
2018-08-19 17:03:53 +02:00
|
|
|
}
|