1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-24 08:22:21 +02:00

Priority routes handled ✌️

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-04-11 10:09:41 -07:00
parent 670e21d80e
commit 4c8797f09e
7 changed files with 40 additions and 40 deletions

View File

@ -41,9 +41,9 @@ func TestEchoIndex(t *testing.T) {
func TestEchoStatic(t *testing.T) {
e := New()
e.Static("/js", "example/public/js")
e.Static("/scripts", "example/public/scripts")
w := httptest.NewRecorder()
r, _ := http.NewRequest(GET, "/js/main.js", nil)
r, _ := http.NewRequest(GET, "/scripts/main.js", nil)
e.ServeHTTP(w, r)
if w.Code != 200 {
t.Errorf("status code should be 200, found %d", w.Code)

View File

@ -83,7 +83,7 @@ func main() {
e.Index("public/index.html")
// Serve static files
e.Static("/js", "public/js")
e.Static("/scripts", "public/scripts")
//************//
// Routes //
@ -97,7 +97,7 @@ func main() {
//***************//
t := &Template{
// Cached templates
templates: template.Must(template.ParseFiles("public/tpl/welcome.tpl")),
templates: template.Must(template.ParseFiles("public/views/welcome.html")),
}
e.Renderer(t)
e.Get("/welcome", welcome)

View File

@ -5,7 +5,7 @@
<title>Echo</title>
</head>
<body>
Hello, Echo!
<script src="/js/main.js"></script>
Echo!
<script src="/scripts/main.js"></script>
</body>
</html>

View File

@ -1 +0,0 @@
console.log("Hello, Echo!")

View File

@ -0,0 +1 @@
console.log("Echo!")

View File

@ -0,0 +1 @@
{{define "welcome"}}Hello, {{.}}!{{end}}

View File

@ -162,6 +162,7 @@ func (r *router) Find(method, path string, params Params) (h HandlerFunc, echo *
search := path
n := 0 // Param count
// Search order static > param > catch-all
for {
if search == "" || search == cn.prefix {
// Found
@ -170,45 +171,43 @@ func (r *router) Find(method, path string, params Params) (h HandlerFunc, echo *
return
}
pl := len(cn.prefix)
l := lcp(search, cn.prefix)
search = search[l:]
if l == pl {
search = search[l:]
if cn.has == pnode {
// Param node
cn = cn.edges[0]
i, l := 0, len(search)
for ; i < l && search[i] != '/'; i++ {
}
p := params[:n+1]
p[n].Name = cn.prefix[1:]
p[n].Value = search[:i]
n++
search = search[i:]
} else if cn.has == cnode {
// Catch-all node
cn = cn.edges[0]
p := params[:n+1]
p[n].Name = "_name"
p[n].Value = search
search = "" // End search
}
// Search complete
if len(search) == 0 {
continue
}
// Static node
e := cn.findEdge(search[0])
if e != nil {
// Go deeper
e := cn.findEdge(search[0])
if e == nil {
// Not found
return
}
cn = e
continue
}
// Param node
e = cn.findEdge(':')
if e != nil {
cn = e
i, l := 0, len(search)
for ; i < l && search[i] != '/'; i++ {
}
p := params[:n+1]
p[n].Name = cn.prefix[1:]
p[n].Value = search[:i]
n++
search = search[i:]
continue
}
// Catch-all node
e = cn.findEdge('*')
if e != nil {
cn = e
p := params[:n+1]
p[n].Name = "_name"
p[n].Value = search
search = "" // End search
continue
}
return
}
}