forked from mirror/gin
Renames []HandleFunc to HandlersChain
This commit is contained in:
parent
79131ac84d
commit
eb3e9293ed
|
@ -59,7 +59,7 @@ type Context struct {
|
||||||
Writer ResponseWriter
|
Writer ResponseWriter
|
||||||
|
|
||||||
Params Params
|
Params Params
|
||||||
handlers []HandlerFunc
|
handlers HandlersChain
|
||||||
index int8
|
index int8
|
||||||
|
|
||||||
Engine *Engine
|
Engine *Engine
|
||||||
|
|
|
@ -73,7 +73,7 @@ func TestContextCopy(t *testing.T) {
|
||||||
c, _, _ := createTestContext()
|
c, _, _ := createTestContext()
|
||||||
c.index = 2
|
c.index = 2
|
||||||
c.Request, _ = http.NewRequest("POST", "/hola", nil)
|
c.Request, _ = http.NewRequest("POST", "/hola", nil)
|
||||||
c.handlers = []HandlerFunc{func(c *Context) {}}
|
c.handlers = HandlersChain{func(c *Context) {}}
|
||||||
c.Params = Params{Param{Key: "foo", Value: "bar"}}
|
c.Params = Params{Param{Key: "foo", Value: "bar"}}
|
||||||
c.Set("foo", "bar")
|
c.Set("foo", "bar")
|
||||||
|
|
||||||
|
|
2
debug.go
2
debug.go
|
@ -15,7 +15,7 @@ func IsDebugging() bool {
|
||||||
return ginMode == debugCode
|
return ginMode == debugCode
|
||||||
}
|
}
|
||||||
|
|
||||||
func debugRoute(httpMethod, absolutePath string, handlers []HandlerFunc) {
|
func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {
|
||||||
if IsDebugging() {
|
if IsDebugging() {
|
||||||
nuHandlers := len(handlers)
|
nuHandlers := len(handlers)
|
||||||
handlerName := nameOfFunction(handlers[nuHandlers-1])
|
handlerName := nameOfFunction(handlers[nuHandlers-1])
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
// func debugRoute(httpMethod, absolutePath string, handlers []HandlerFunc) {
|
// func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {
|
||||||
// func debugPrint(format string, values ...interface{}) {
|
// func debugPrint(format string, values ...interface{}) {
|
||||||
|
|
||||||
func TestIsDebugging(t *testing.T) {
|
func TestIsDebugging(t *testing.T) {
|
||||||
|
|
13
gin.go
13
gin.go
|
@ -17,17 +17,18 @@ var default404Body = []byte("404 page not found")
|
||||||
var default405Body = []byte("405 method not allowed")
|
var default405Body = []byte("405 method not allowed")
|
||||||
|
|
||||||
type (
|
type (
|
||||||
HandlerFunc func(*Context)
|
HandlerFunc func(*Context)
|
||||||
|
HandlersChain []HandlerFunc
|
||||||
|
|
||||||
// Represents the web framework, it wraps the blazing fast httprouter multiplexer and a list of global middlewares.
|
// Represents the web framework, it wraps the blazing fast httprouter multiplexer and a list of global middlewares.
|
||||||
Engine struct {
|
Engine struct {
|
||||||
RouterGroup
|
RouterGroup
|
||||||
HTMLRender render.Render
|
HTMLRender render.Render
|
||||||
pool sync.Pool
|
pool sync.Pool
|
||||||
allNoRoute []HandlerFunc
|
allNoRoute HandlersChain
|
||||||
allNoMethod []HandlerFunc
|
allNoMethod HandlersChain
|
||||||
noRoute []HandlerFunc
|
noRoute HandlersChain
|
||||||
noMethod []HandlerFunc
|
noMethod HandlersChain
|
||||||
trees map[string]*node
|
trees map[string]*node
|
||||||
|
|
||||||
// Enables automatic redirection if the current route can't be matched but a
|
// Enables automatic redirection if the current route can't be matched but a
|
||||||
|
@ -136,7 +137,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 []HandlerFunc) {
|
func (engine *Engine) handle(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("", "/", []HandlerFunc{func(_ *Context) {}}) })
|
assert.Panics(t, func() { router.handle("", "/", HandlersChain{func(_ *Context) {}}) })
|
||||||
assert.Panics(t, func() { router.handle("GET", "", []HandlerFunc{func(_ *Context) {}}) })
|
assert.Panics(t, func() { router.handle("GET", "", HandlersChain{func(_ *Context) {}}) })
|
||||||
assert.Panics(t, func() { router.handle("GET", "/", []HandlerFunc{}) })
|
assert.Panics(t, func() { router.handle("GET", "/", HandlersChain{}) })
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateDefaultRouter(t *testing.T) {
|
func TestCreateDefaultRouter(t *testing.T) {
|
||||||
|
|
|
@ -286,7 +286,7 @@ func TestGithubAPI(t *testing.T) {
|
||||||
router := New()
|
router := New()
|
||||||
|
|
||||||
for _, route := range githubAPI {
|
for _, route := range githubAPI {
|
||||||
router.Handle(route.method, route.path, []HandlerFunc{func(c *Context) {
|
router.Handle(route.method, route.path, HandlersChain{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
|
||||||
|
|
|
@ -12,7 +12,7 @@ import (
|
||||||
// Used internally to configure router, a RouterGroup is associated with a prefix
|
// Used internally to configure router, a RouterGroup is associated with a prefix
|
||||||
// and an array of handlers (middlewares)
|
// and an array of handlers (middlewares)
|
||||||
type RouterGroup struct {
|
type RouterGroup struct {
|
||||||
Handlers []HandlerFunc
|
Handlers HandlersChain
|
||||||
absolutePath string
|
absolutePath string
|
||||||
engine *Engine
|
engine *Engine
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,7 @@ 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 []HandlerFunc) {
|
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)
|
||||||
debugRoute(httpMethod, absolutePath, handlers)
|
debugRoute(httpMethod, absolutePath, handlers)
|
||||||
|
@ -117,9 +117,9 @@ func (group *RouterGroup) createStaticHandler(absolutePath, root string) func(*C
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (group *RouterGroup) combineHandlers(handlers []HandlerFunc) []HandlerFunc {
|
func (group *RouterGroup) combineHandlers(handlers HandlersChain) HandlersChain {
|
||||||
finalSize := len(group.Handlers) + len(handlers)
|
finalSize := len(group.Handlers) + len(handlers)
|
||||||
mergedHandlers := make([]HandlerFunc, finalSize)
|
mergedHandlers := make(HandlersChain, finalSize)
|
||||||
copy(mergedHandlers, group.Handlers)
|
copy(mergedHandlers, group.Handlers)
|
||||||
copy(mergedHandlers[len(group.Handlers):], handlers)
|
copy(mergedHandlers[len(group.Handlers):], handlers)
|
||||||
return mergedHandlers
|
return mergedHandlers
|
||||||
|
|
|
@ -27,7 +27,7 @@ func testRouteOK(method string, t *testing.T) {
|
||||||
// SETUP
|
// SETUP
|
||||||
passed := false
|
passed := false
|
||||||
r := New()
|
r := New()
|
||||||
r.Handle(method, "/test", []HandlerFunc{func(c *Context) {
|
r.Handle(method, "/test", HandlersChain{func(c *Context) {
|
||||||
passed = true
|
passed = true
|
||||||
}})
|
}})
|
||||||
// RUN
|
// RUN
|
||||||
|
@ -43,7 +43,7 @@ func testRouteNotOK(method string, t *testing.T) {
|
||||||
// SETUP
|
// SETUP
|
||||||
passed := false
|
passed := false
|
||||||
router := New()
|
router := New()
|
||||||
router.Handle(method, "/test_2", []HandlerFunc{func(c *Context) {
|
router.Handle(method, "/test_2", HandlersChain{func(c *Context) {
|
||||||
passed = true
|
passed = true
|
||||||
}})
|
}})
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ func testRouteNotOK2(method string, t *testing.T) {
|
||||||
} else {
|
} else {
|
||||||
methodRoute = "POST"
|
methodRoute = "POST"
|
||||||
}
|
}
|
||||||
router.Handle(methodRoute, "/test", []HandlerFunc{func(c *Context) {
|
router.Handle(methodRoute, "/test", HandlersChain{func(c *Context) {
|
||||||
passed = true
|
passed = true
|
||||||
}})
|
}})
|
||||||
|
|
||||||
|
|
8
tree.go
8
tree.go
|
@ -45,7 +45,7 @@ type node struct {
|
||||||
maxParams uint8
|
maxParams uint8
|
||||||
indices string
|
indices string
|
||||||
children []*node
|
children []*node
|
||||||
handlers []HandlerFunc
|
handlers HandlersChain
|
||||||
priority uint32
|
priority uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,7 +77,7 @@ func (n *node) incrementChildPrio(pos int) int {
|
||||||
|
|
||||||
// addRoute adds a node with the given handle to the path.
|
// addRoute adds a node with the given handle to the path.
|
||||||
// Not concurrency-safe!
|
// Not concurrency-safe!
|
||||||
func (n *node) addRoute(path string, handlers []HandlerFunc) {
|
func (n *node) addRoute(path string, handlers HandlersChain) {
|
||||||
fullPath := path
|
fullPath := path
|
||||||
n.priority++
|
n.priority++
|
||||||
numParams := countParams(path)
|
numParams := countParams(path)
|
||||||
|
@ -198,7 +198,7 @@ func (n *node) addRoute(path string, handlers []HandlerFunc) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *node) insertChild(numParams uint8, path string, fullPath string, handlers []HandlerFunc) {
|
func (n *node) insertChild(numParams uint8, path string, fullPath string, handlers HandlersChain) {
|
||||||
var offset int // already handled bytes of the path
|
var offset int // already handled bytes of the path
|
||||||
|
|
||||||
// find prefix until first wildcard (beginning with ':'' or '*'')
|
// find prefix until first wildcard (beginning with ':'' or '*'')
|
||||||
|
@ -316,7 +316,7 @@ func (n *node) insertChild(numParams uint8, path string, fullPath string, handle
|
||||||
// If no handle can be found, a TSR (trailing slash redirect) recommendation is
|
// If no handle can be found, a TSR (trailing slash redirect) recommendation is
|
||||||
// made if a handle exists with an extra (without the) trailing slash for the
|
// made if a handle exists with an extra (without the) trailing slash for the
|
||||||
// given path.
|
// given path.
|
||||||
func (n *node) getValue(path string, po Params) (handlers []HandlerFunc, p Params, tsr bool) {
|
func (n *node) getValue(path string, po Params) (handlers HandlersChain, p Params, tsr bool) {
|
||||||
p = po
|
p = po
|
||||||
walk: // Outer loop for walking the tree
|
walk: // Outer loop for walking the tree
|
||||||
for {
|
for {
|
||||||
|
|
|
@ -24,8 +24,8 @@ func printChildren(n *node, prefix string) {
|
||||||
// Used as a workaround since we can't compare functions or their adresses
|
// Used as a workaround since we can't compare functions or their adresses
|
||||||
var fakeHandlerValue string
|
var fakeHandlerValue string
|
||||||
|
|
||||||
func fakeHandler(val string) []HandlerFunc {
|
func fakeHandler(val string) HandlersChain {
|
||||||
return []HandlerFunc{func(c *Context) {
|
return HandlersChain{func(c *Context) {
|
||||||
fakeHandlerValue = val
|
fakeHandlerValue = val
|
||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue