mirror of
https://github.com/labstack/echo.git
synced 2024-11-24 08:22:21 +02:00
Cleanup on router
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
12bd049b0d
commit
2342df3e61
42
router.go
42
router.go
@ -25,9 +25,9 @@ type (
|
||||
)
|
||||
|
||||
const (
|
||||
snode ntype = iota // Static node
|
||||
pnode // Param node
|
||||
anode // Catch-all node
|
||||
snode ntype = 1 + iota // Static node
|
||||
pnode // Param node
|
||||
cnode // Catch-all node
|
||||
)
|
||||
|
||||
func NewRouter(e *Echo) (r *router) {
|
||||
@ -51,12 +51,13 @@ func (r *router) Add(method, path string, h HandlerFunc, echo *Echo) {
|
||||
for ; i < l && path[i] != '/'; i++ {
|
||||
}
|
||||
if i == l {
|
||||
r.insert(method, path[:i], h, snode, echo)
|
||||
r.insert(method, path[:i], h, 0, echo)
|
||||
return
|
||||
}
|
||||
r.insert(method, path[:i], nil, snode, echo)
|
||||
r.insert(method, path[:i], nil, 0, echo)
|
||||
} else if path[i] == '*' {
|
||||
r.insert(method, path[:i], h, anode, echo)
|
||||
r.insert(method, path[:i], nil, cnode, echo)
|
||||
r.insert(method, path[:l], h, 0, echo)
|
||||
}
|
||||
}
|
||||
r.insert(method, path, h, snode, echo)
|
||||
@ -80,16 +81,15 @@ func (r *router) insert(method, path string, h HandlerFunc, has ntype, echo *Ech
|
||||
cn.handler = h
|
||||
cn.echo = echo
|
||||
}
|
||||
return
|
||||
} else if l < pl {
|
||||
// Split the node
|
||||
// Split node
|
||||
n := newNode(cn.prefix[l:], cn.has, cn.handler, cn.edges, cn.echo)
|
||||
cn.edges = edges{n} // Add to parent
|
||||
|
||||
// Reset parent node
|
||||
cn.label = cn.prefix[0]
|
||||
cn.prefix = cn.prefix[:l]
|
||||
cn.has = snode
|
||||
cn.has = 0
|
||||
cn.handler = nil
|
||||
cn.echo = nil
|
||||
|
||||
@ -98,29 +98,29 @@ func (r *router) insert(method, path string, h HandlerFunc, has ntype, echo *Ech
|
||||
cn.handler = h
|
||||
cn.echo = echo
|
||||
} else {
|
||||
// Need to create a node
|
||||
// Create child node
|
||||
n = newNode(search[l:], has, h, edges{}, echo)
|
||||
cn.edges = append(cn.edges, n)
|
||||
}
|
||||
break
|
||||
} else if l < sl {
|
||||
search = search[l:]
|
||||
e := cn.findEdge(search[0])
|
||||
if e == nil {
|
||||
n := newNode(search, has, h, edges{}, echo)
|
||||
cn.edges = append(cn.edges, n)
|
||||
break
|
||||
} else {
|
||||
if e != nil {
|
||||
// Go deeper
|
||||
cn = e
|
||||
continue
|
||||
}
|
||||
// Create child node
|
||||
n := newNode(search, has, h, edges{}, echo)
|
||||
cn.edges = append(cn.edges, n)
|
||||
} else {
|
||||
// Node already exists
|
||||
if h != nil {
|
||||
cn.handler = h
|
||||
cn.echo = echo
|
||||
}
|
||||
break
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@ -179,8 +179,7 @@ func (r *router) Find(method, path string) (h HandlerFunc, c *Context, echo *Ech
|
||||
if cn.has == pnode {
|
||||
// Param node
|
||||
cn = cn.edges[0]
|
||||
i := 0
|
||||
l = len(search)
|
||||
i, l := 0, len(search)
|
||||
for ; i < l && search[i] != '/'; i++ {
|
||||
}
|
||||
p := c.params[:n+1]
|
||||
@ -188,8 +187,9 @@ func (r *router) Find(method, path string) (h HandlerFunc, c *Context, echo *Ech
|
||||
p[n].Value = search[:i]
|
||||
n++
|
||||
search = search[i:]
|
||||
} else if cn.has == anode {
|
||||
} else if cn.has == cnode {
|
||||
// Catch-all node
|
||||
cn = cn.edges[0]
|
||||
p := c.params[:n+1]
|
||||
p[n].Name = "_name"
|
||||
p[n].Value = search
|
||||
@ -201,7 +201,7 @@ func (r *router) Find(method, path string) (h HandlerFunc, c *Context, echo *Ech
|
||||
continue
|
||||
}
|
||||
|
||||
// Dig more
|
||||
// Go deeper
|
||||
e := cn.findEdge(search[0])
|
||||
if e == nil {
|
||||
// Not found
|
||||
|
@ -277,7 +277,7 @@ var api = []route{
|
||||
|
||||
func TestRouterStatic(t *testing.T) {
|
||||
r := New().Router
|
||||
r.Add(GET, "/folders/files/echo.gif", func(c *Context) {}, nil)
|
||||
r.Add(GET, "/folders/files/echo.gif", func(*Context) {}, nil)
|
||||
h, _, _ := r.Find(GET, "/folders/files/echo.gif")
|
||||
if h == nil {
|
||||
t.Fatal("handle not found")
|
||||
@ -286,7 +286,7 @@ func TestRouterStatic(t *testing.T) {
|
||||
|
||||
func TestRouterParam(t *testing.T) {
|
||||
r := New().Router
|
||||
r.Add(GET, "/users/:id", func(c *Context) {}, nil)
|
||||
r.Add(GET, "/users/:id", func(*Context) {}, nil)
|
||||
h, c, _ := r.Find(GET, "/users/1")
|
||||
if h == nil {
|
||||
t.Fatal("handle not found")
|
||||
@ -298,7 +298,7 @@ func TestRouterParam(t *testing.T) {
|
||||
|
||||
func TestRouterTwoParam(t *testing.T) {
|
||||
r := New().Router
|
||||
r.Add(GET, "/users/:uid/files/:fid", func(c *Context) {}, nil)
|
||||
r.Add(GET, "/users/:uid/files/:fid", func(*Context) {}, nil)
|
||||
h, c, _ := r.Find(GET, "/users/1/files/1")
|
||||
if h == nil {
|
||||
t.Fatal("handle not found")
|
||||
@ -313,7 +313,7 @@ func TestRouterTwoParam(t *testing.T) {
|
||||
|
||||
func TestRouterCatchAll(t *testing.T) {
|
||||
r := New().Router
|
||||
r.Add(GET, "/static/*", func(c *Context) {}, nil)
|
||||
r.Add(GET, "/static/*", func(*Context) {}, nil)
|
||||
h, _, _ := r.Find(GET, "/static/*")
|
||||
if h == nil {
|
||||
t.Fatal("handle not found")
|
||||
@ -322,7 +322,7 @@ func TestRouterCatchAll(t *testing.T) {
|
||||
|
||||
func TestRouterMicroParam(t *testing.T) {
|
||||
r := New().Router
|
||||
r.Add(GET, "/:a/:b/:c", func(c *Context) {}, nil)
|
||||
r.Add(GET, "/:a/:b/:c", func(*Context) {}, nil)
|
||||
h, c, _ := r.Find(GET, "/1/2/3")
|
||||
if h == nil {
|
||||
t.Fatal("handle not found")
|
||||
@ -358,7 +358,7 @@ func TestRouterAPI(t *testing.T) {
|
||||
|
||||
func TestRouterServeHTTP(t *testing.T) {
|
||||
r := New().Router
|
||||
r.Add(GET, "/users", func(c *Context) {}, nil)
|
||||
r.Add(GET, "/users", func(*Context) {}, nil)
|
||||
|
||||
// OK
|
||||
req, _ := http.NewRequest(GET, "/users", nil)
|
||||
|
Loading…
Reference in New Issue
Block a user