mirror of https://github.com/gin-gonic/gin.git
Better API for RouteGroup.Handle()
This commit is contained in:
parent
53329e4646
commit
022304e7d9
2
gin.go
2
gin.go
|
@ -140,7 +140,7 @@ func (engine *Engine) rebuild405Handlers() {
|
||||||
engine.allNoMethod = engine.combineHandlers(engine.noMethod)
|
engine.allNoMethod = engine.combineHandlers(engine.noMethod)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) handle(method, path string, handlers HandlersChain) {
|
func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
|
||||||
if path[0] != '/' {
|
if path[0] != '/' {
|
||||||
panic("path must begin with '/'")
|
panic("path must begin with '/'")
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,9 +29,9 @@ func TestCreateEngine(t *testing.T) {
|
||||||
assert.True(t, router.RedirectFixedPath)
|
assert.True(t, router.RedirectFixedPath)
|
||||||
assert.True(t, router.HandleMethodNotAllowed)
|
assert.True(t, router.HandleMethodNotAllowed)
|
||||||
|
|
||||||
assert.Panics(t, func() { router.handle("", "/", HandlersChain{func(_ *Context) {}}) })
|
assert.Panics(t, func() { router.addRoute("", "/", HandlersChain{func(_ *Context) {}}) })
|
||||||
assert.Panics(t, func() { router.handle("GET", "a", HandlersChain{func(_ *Context) {}}) })
|
assert.Panics(t, func() { router.addRoute("GET", "a", HandlersChain{func(_ *Context) {}}) })
|
||||||
assert.Panics(t, func() { router.handle("GET", "/", HandlersChain{}) })
|
assert.Panics(t, func() { router.addRoute("GET", "/", HandlersChain{}) })
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateDefaultRouter(t *testing.T) {
|
func TestCreateDefaultRouter(t *testing.T) {
|
||||||
|
|
|
@ -286,13 +286,13 @@ func TestGithubAPI(t *testing.T) {
|
||||||
router := New()
|
router := New()
|
||||||
|
|
||||||
for _, route := range githubAPI {
|
for _, route := range githubAPI {
|
||||||
router.Handle(route.method, route.path, HandlersChain{func(c *Context) {
|
router.Handle(route.method, route.path, func(c *Context) {
|
||||||
output := H{"status": "good"}
|
output := H{"status": "good"}
|
||||||
for _, param := range c.Params {
|
for _, param := range c.Params {
|
||||||
output[param.Key] = param.Value
|
output[param.Key] = param.Value
|
||||||
}
|
}
|
||||||
c.JSON(200, output)
|
c.JSON(200, output)
|
||||||
}})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, route := range githubAPI {
|
for _, route := range githubAPI {
|
||||||
|
|
|
@ -42,72 +42,76 @@ func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *R
|
||||||
// This function is intended for bulk loading and to allow the usage of less
|
// This function is intended for bulk loading and to allow the usage of less
|
||||||
// frequently used, non-standardized or custom methods (e.g. for internal
|
// frequently used, non-standardized or custom methods (e.g. for internal
|
||||||
// communication with a proxy).
|
// communication with a proxy).
|
||||||
func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers HandlersChain) {
|
func (group *RouterGroup) handle(httpMethod, relativePath string, handlers HandlersChain) {
|
||||||
absolutePath := group.calculateAbsolutePath(relativePath)
|
absolutePath := group.calculateAbsolutePath(relativePath)
|
||||||
handlers = group.combineHandlers(handlers)
|
handlers = group.combineHandlers(handlers)
|
||||||
debugPrintRoute(httpMethod, absolutePath, handlers)
|
debugPrintRoute(httpMethod, absolutePath, handlers)
|
||||||
group.engine.handle(httpMethod, absolutePath, handlers)
|
group.engine.addRoute(httpMethod, absolutePath, handlers)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) {
|
||||||
|
group.handle(httpMethod, relativePath, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST is a shortcut for router.Handle("POST", path, handle)
|
// POST is a shortcut for router.Handle("POST", path, handle)
|
||||||
func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) {
|
func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) {
|
||||||
group.Handle("POST", relativePath, handlers)
|
group.handle("POST", relativePath, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET is a shortcut for router.Handle("GET", path, handle)
|
// GET is a shortcut for router.Handle("GET", path, handle)
|
||||||
func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) {
|
func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) {
|
||||||
group.Handle("GET", relativePath, handlers)
|
group.handle("GET", relativePath, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DELETE is a shortcut for router.Handle("DELETE", path, handle)
|
// DELETE is a shortcut for router.Handle("DELETE", path, handle)
|
||||||
func (group *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc) {
|
func (group *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc) {
|
||||||
group.Handle("DELETE", relativePath, handlers)
|
group.handle("DELETE", relativePath, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PATCH is a shortcut for router.Handle("PATCH", path, handle)
|
// PATCH is a shortcut for router.Handle("PATCH", path, handle)
|
||||||
func (group *RouterGroup) PATCH(relativePath string, handlers ...HandlerFunc) {
|
func (group *RouterGroup) PATCH(relativePath string, handlers ...HandlerFunc) {
|
||||||
group.Handle("PATCH", relativePath, handlers)
|
group.handle("PATCH", relativePath, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PUT is a shortcut for router.Handle("PUT", path, handle)
|
// PUT is a shortcut for router.Handle("PUT", path, handle)
|
||||||
func (group *RouterGroup) PUT(relativePath string, handlers ...HandlerFunc) {
|
func (group *RouterGroup) PUT(relativePath string, handlers ...HandlerFunc) {
|
||||||
group.Handle("PUT", relativePath, handlers)
|
group.handle("PUT", relativePath, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
|
// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
|
||||||
func (group *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) {
|
func (group *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) {
|
||||||
group.Handle("OPTIONS", relativePath, handlers)
|
group.handle("OPTIONS", relativePath, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HEAD is a shortcut for router.Handle("HEAD", path, handle)
|
// HEAD is a shortcut for router.Handle("HEAD", path, handle)
|
||||||
func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) {
|
func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) {
|
||||||
group.Handle("HEAD", relativePath, handlers)
|
group.handle("HEAD", relativePath, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
// LINK is a shortcut for router.Handle("LINK", path, handle)
|
// LINK is a shortcut for router.Handle("LINK", path, handle)
|
||||||
func (group *RouterGroup) LINK(relativePath string, handlers ...HandlerFunc) {
|
func (group *RouterGroup) LINK(relativePath string, handlers ...HandlerFunc) {
|
||||||
group.Handle("LINK", relativePath, handlers)
|
group.handle("LINK", relativePath, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UNLINK is a shortcut for router.Handle("UNLINK", path, handle)
|
// UNLINK is a shortcut for router.Handle("UNLINK", path, handle)
|
||||||
func (group *RouterGroup) UNLINK(relativePath string, handlers ...HandlerFunc) {
|
func (group *RouterGroup) UNLINK(relativePath string, handlers ...HandlerFunc) {
|
||||||
group.Handle("UNLINK", relativePath, handlers)
|
group.handle("UNLINK", relativePath, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) {
|
func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) {
|
||||||
// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, WS, LINK, UNLINK, TRACE
|
// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, WS, LINK, UNLINK, TRACE
|
||||||
group.Handle("GET", relativePath, handlers)
|
group.handle("GET", relativePath, handlers)
|
||||||
group.Handle("POST", relativePath, handlers)
|
group.handle("POST", relativePath, handlers)
|
||||||
group.Handle("PUT", relativePath, handlers)
|
group.handle("PUT", relativePath, handlers)
|
||||||
group.Handle("PATCH", relativePath, handlers)
|
group.handle("PATCH", relativePath, handlers)
|
||||||
group.Handle("HEAD", relativePath, handlers)
|
group.handle("HEAD", relativePath, handlers)
|
||||||
group.Handle("OPTIONS", relativePath, handlers)
|
group.handle("OPTIONS", relativePath, handlers)
|
||||||
group.Handle("DELETE", relativePath, handlers)
|
group.handle("DELETE", relativePath, handlers)
|
||||||
group.Handle("CONNECT", relativePath, handlers)
|
group.handle("CONNECT", relativePath, handlers)
|
||||||
group.Handle("WS", relativePath, handlers)
|
group.handle("WS", relativePath, handlers)
|
||||||
group.Handle("LINK", relativePath, handlers)
|
group.handle("LINK", relativePath, handlers)
|
||||||
group.Handle("UNLINK", relativePath, handlers)
|
group.handle("UNLINK", relativePath, handlers)
|
||||||
group.Handle("TRACE", relativePath, handlers)
|
group.handle("TRACE", relativePath, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Static serves files from the given file system root.
|
// Static serves files from the given file system root.
|
||||||
|
|
|
@ -30,9 +30,9 @@ func testRouteOK(method string, t *testing.T) {
|
||||||
r.Any("/test2", func(c *Context) {
|
r.Any("/test2", func(c *Context) {
|
||||||
passedAny = true
|
passedAny = true
|
||||||
})
|
})
|
||||||
r.Handle(method, "/test", HandlersChain{func(c *Context) {
|
r.Handle(method, "/test", func(c *Context) {
|
||||||
passed = true
|
passed = true
|
||||||
}})
|
})
|
||||||
|
|
||||||
w := performRequest(r, method, "/test")
|
w := performRequest(r, method, "/test")
|
||||||
assert.True(t, passed)
|
assert.True(t, passed)
|
||||||
|
@ -47,9 +47,9 @@ func testRouteOK(method string, t *testing.T) {
|
||||||
func testRouteNotOK(method string, t *testing.T) {
|
func testRouteNotOK(method string, t *testing.T) {
|
||||||
passed := false
|
passed := false
|
||||||
router := New()
|
router := New()
|
||||||
router.Handle(method, "/test_2", HandlersChain{func(c *Context) {
|
router.Handle(method, "/test_2", func(c *Context) {
|
||||||
passed = true
|
passed = true
|
||||||
}})
|
})
|
||||||
|
|
||||||
w := performRequest(router, method, "/test")
|
w := performRequest(router, method, "/test")
|
||||||
|
|
||||||
|
@ -67,9 +67,9 @@ func testRouteNotOK2(method string, t *testing.T) {
|
||||||
} else {
|
} else {
|
||||||
methodRoute = "POST"
|
methodRoute = "POST"
|
||||||
}
|
}
|
||||||
router.Handle(methodRoute, "/test", HandlersChain{func(c *Context) {
|
router.Handle(methodRoute, "/test", func(c *Context) {
|
||||||
passed = true
|
passed = true
|
||||||
}})
|
})
|
||||||
|
|
||||||
w := performRequest(router, method, "/test")
|
w := performRequest(router, method, "/test")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue