Skip to content

Commit

Permalink
Merge pull request #10 from bmf-san/1.0.2
Browse files Browse the repository at this point in the history
1.0.2
  • Loading branch information
bmf-san authored May 31, 2020
2 parents dafa3ca + 0a5f451 commit 9dcf49e
Show file tree
Hide file tree
Showing 5 changed files with 686 additions and 77 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ You can use named parameters like this.
```go
r := goblin.NewRouter()

r.GET(`/foo/:id/`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.GET(`/foo/:id`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := goblin.GetParam(r.Context(), "id")
fmt.Fprintf(w, "/foo/%v/", id)
fmt.Fprintf(w, "/foo/%v", id)
}))

r.POST(`/foo/:name/`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.POST(`/foo/:name`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
name := goblin.GetParam(r.Context(), "name")
fmt.Fprintf(w, "/foo/%v/", name)
fmt.Fprintf(w, "/foo/%v", name)
}))
```

Expand All @@ -60,9 +60,9 @@ You can also use named parameter with regular expression like this.
`[name:pattern]`

```go
r.GET(`/foo/:id[^\d+$]/`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.GET(`/foo/:id[^\d+$]`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := goblin.GetParam(r.Context(), "id")
fmt.Fprintf(w, "/foo/%v/", id)
fmt.Fprintf(w, "/foo/%v", id)
}))
```

Expand All @@ -76,14 +76,14 @@ A routing pattern matching priority depends on an order of routing definition.
```go
r := goblin.NewRouter()

r.GET(`/foo/:id/`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `/foo/:id/`)
r.GET(`/foo/:id`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `/foo/:id`)
}))
r.GET(`/foo/:id[^\d+$]/`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `/foo/:id[^\d+$]/`)
r.GET(`/foo/:id[^\d+$]`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `/foo/:id[^\d+$]`)
}))
r.GET(`/foo/:id[^\w+$]`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `/foo/:id[^\w+$]/`)
fmt.Fprintf(w, `/foo/:id[^\w+$]`)
}))
```

Expand Down Expand Up @@ -129,7 +129,7 @@ func main() {
fmt.Fprintf(w, "/foo/%v/%v", id, name)
}))

http.ListenAndServe(":8000", r)
http.ListenAndServe(":9999", r)
}
```

Expand Down
4 changes: 2 additions & 2 deletions _examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func main() {
r.GET(`/`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "/")
}))
r.GET(`/foo/`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.GET(`/foo`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "/foo")
}))
r.GET(`/foo/bar`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -38,5 +38,5 @@ func main() {
fmt.Fprintf(w, "/foo/%v/%v", id, name)
}))

http.ListenAndServe(":8000", r)
http.ListenAndServe(":9999", r)
}
17 changes: 15 additions & 2 deletions trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ func (t *Tree) Search(method string, path string) (*Result, error) {
// 2 /foo/:id[^\d+$]
// 3 /foo/:id[^\w+$]
// priority is 1, 2, 3
if len(curNode.children) == 0 {
return &Result{}, errors.New("handler is not regsitered")
}

count := 0
for c := range curNode.children {
if string([]rune(c)[0]) == paramDelimiter {
ptn := getPattern(c)
Expand All @@ -143,17 +148,25 @@ func (t *Tree) Search(method string, path string) (*Result, error) {
})

curNode = curNode.children[c]
count++
break
} else {
return nil, errors.New("param does not match")
return &Result{}, errors.New("param does not match")
}
}

count++

// If no match is found until the last loop.
if count == len(curNode.children) {
return &Result{}, errors.New("handler is not regsitered")
}
}
}
}

if curNode.handler == nil {
return nil, errors.New("handler is not registered")
return &Result{}, errors.New("handler is not registered")
}

return &Result{
Expand Down
Loading

0 comments on commit 9dcf49e

Please sign in to comment.