1
0
mirror of https://github.com/labstack/echo.git synced 2025-07-15 01:34:53 +02:00

Cleanup on router

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana
2015-04-08 14:40:49 -07:00
parent 12bd049b0d
commit 2342df3e61
2 changed files with 27 additions and 27 deletions

View File

@ -25,9 +25,9 @@ type (
) )
const ( const (
snode ntype = iota // Static node snode ntype = 1 + iota // Static node
pnode // Param node pnode // Param node
anode // Catch-all node cnode // Catch-all node
) )
func NewRouter(e *Echo) (r *router) { 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++ { for ; i < l && path[i] != '/'; i++ {
} }
if i == l { if i == l {
r.insert(method, path[:i], h, snode, echo) r.insert(method, path[:i], h, 0, echo)
return return
} }
r.insert(method, path[:i], nil, snode, echo) r.insert(method, path[:i], nil, 0, echo)
} else if path[i] == '*' { } 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) 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.handler = h
cn.echo = echo cn.echo = echo
} }
return
} else if l < pl { } else if l < pl {
// Split the node // Split node
n := newNode(cn.prefix[l:], cn.has, cn.handler, cn.edges, cn.echo) n := newNode(cn.prefix[l:], cn.has, cn.handler, cn.edges, cn.echo)
cn.edges = edges{n} // Add to parent cn.edges = edges{n} // Add to parent
// Reset parent node // Reset parent node
cn.label = cn.prefix[0] cn.label = cn.prefix[0]
cn.prefix = cn.prefix[:l] cn.prefix = cn.prefix[:l]
cn.has = snode cn.has = 0
cn.handler = nil cn.handler = nil
cn.echo = 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.handler = h
cn.echo = echo cn.echo = echo
} else { } else {
// Need to create a node // Create child node
n = newNode(search[l:], has, h, edges{}, echo) n = newNode(search[l:], has, h, edges{}, echo)
cn.edges = append(cn.edges, n) cn.edges = append(cn.edges, n)
} }
break
} else if l < sl { } else if l < sl {
search = search[l:] search = search[l:]
e := cn.findEdge(search[0]) e := cn.findEdge(search[0])
if e == nil { if e != nil {
n := newNode(search, has, h, edges{}, echo) // Go deeper
cn.edges = append(cn.edges, n)
break
} else {
cn = e cn = e
continue
} }
// Create child node
n := newNode(search, has, h, edges{}, echo)
cn.edges = append(cn.edges, n)
} else { } else {
// Node already exists // Node already exists
if h != nil { if h != nil {
cn.handler = h cn.handler = h
cn.echo = echo 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 { if cn.has == pnode {
// Param node // Param node
cn = cn.edges[0] cn = cn.edges[0]
i := 0 i, l := 0, len(search)
l = len(search)
for ; i < l && search[i] != '/'; i++ { for ; i < l && search[i] != '/'; i++ {
} }
p := c.params[:n+1] 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] p[n].Value = search[:i]
n++ n++
search = search[i:] search = search[i:]
} else if cn.has == anode { } else if cn.has == cnode {
// Catch-all node // Catch-all node
cn = cn.edges[0]
p := c.params[:n+1] p := c.params[:n+1]
p[n].Name = "_name" p[n].Name = "_name"
p[n].Value = search p[n].Value = search
@ -201,7 +201,7 @@ func (r *router) Find(method, path string) (h HandlerFunc, c *Context, echo *Ech
continue continue
} }
// Dig more // Go deeper
e := cn.findEdge(search[0]) e := cn.findEdge(search[0])
if e == nil { if e == nil {
// Not found // Not found

View File

@ -277,7 +277,7 @@ var api = []route{
func TestRouterStatic(t *testing.T) { func TestRouterStatic(t *testing.T) {
r := New().Router 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") h, _, _ := r.Find(GET, "/folders/files/echo.gif")
if h == nil { if h == nil {
t.Fatal("handle not found") t.Fatal("handle not found")
@ -286,7 +286,7 @@ func TestRouterStatic(t *testing.T) {
func TestRouterParam(t *testing.T) { func TestRouterParam(t *testing.T) {
r := New().Router 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") h, c, _ := r.Find(GET, "/users/1")
if h == nil { if h == nil {
t.Fatal("handle not found") t.Fatal("handle not found")
@ -298,7 +298,7 @@ func TestRouterParam(t *testing.T) {
func TestRouterTwoParam(t *testing.T) { func TestRouterTwoParam(t *testing.T) {
r := New().Router 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") h, c, _ := r.Find(GET, "/users/1/files/1")
if h == nil { if h == nil {
t.Fatal("handle not found") t.Fatal("handle not found")
@ -313,7 +313,7 @@ func TestRouterTwoParam(t *testing.T) {
func TestRouterCatchAll(t *testing.T) { func TestRouterCatchAll(t *testing.T) {
r := New().Router r := New().Router
r.Add(GET, "/static/*", func(c *Context) {}, nil) r.Add(GET, "/static/*", func(*Context) {}, nil)
h, _, _ := r.Find(GET, "/static/*") h, _, _ := r.Find(GET, "/static/*")
if h == nil { if h == nil {
t.Fatal("handle not found") t.Fatal("handle not found")
@ -322,7 +322,7 @@ func TestRouterCatchAll(t *testing.T) {
func TestRouterMicroParam(t *testing.T) { func TestRouterMicroParam(t *testing.T) {
r := New().Router 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") h, c, _ := r.Find(GET, "/1/2/3")
if h == nil { if h == nil {
t.Fatal("handle not found") t.Fatal("handle not found")
@ -358,7 +358,7 @@ func TestRouterAPI(t *testing.T) {
func TestRouterServeHTTP(t *testing.T) { func TestRouterServeHTTP(t *testing.T) {
r := New().Router r := New().Router
r.Add(GET, "/users", func(c *Context) {}, nil) r.Add(GET, "/users", func(*Context) {}, nil)
// OK // OK
req, _ := http.NewRequest(GET, "/users", nil) req, _ := http.NewRequest(GET, "/users", nil)