Skip to content

Commit

Permalink
routing path split fix
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewhoff committed Apr 12, 2020
1 parent 046db70 commit 545f002
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
hooks.db
.env
dbdata
.idea
.vscode
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ dev:
mkdir -p dbdata
go run main.go

tools:
go get github.com/coapcloud/coap-cli/cmd@v0.1.1
mv ${GOPATH}/bin/cmd ${GOPATH}/bin/coap-cli

clean:
rm -rf ./dbdata

Expand Down
12 changes: 7 additions & 5 deletions coap/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"log"
"strings"
"time"

"github.com/coapcloud/coap-hooks-router/hooks"
Expand Down Expand Up @@ -55,7 +56,7 @@ func (r routeTable) ServeCOAP(w coap.ResponseWriter, req *coap.Request) {

log.Printf("req: %+v\n", req)

dest, ok := r.match(req.Msg.Code(), req.Msg.Path())
dest, ok := r.match(req.Msg.Code(), req.Msg.PathString())
if ok {
buf := new(bytes.Buffer)
buf.Write(req.Msg.Payload())
Expand All @@ -82,16 +83,17 @@ func (r routeTable) ServeCOAP(w coap.ResponseWriter, req *coap.Request) {
}
}

func (r *routeTable) match(verb codes.Code, pathComponents []string) (hooks.Hook, bool) {
func (r *routeTable) match(verb codes.Code, path string) (hooks.Hook, bool) {
r.RLock()
defer r.RUnlock()

if len(pathComponents) < 2 {
log.Printf("couldn't find route from path: %v\n", pathComponents)
toks := strings.Split(path, "/")
if len(toks) < 3 {
log.Printf("couldn't find route from path: %v\n", path)
return hooks.Hook{}, false
}

key := routeKey(verb, pathComponents[0], pathComponents[1])
key := routeKey(verb, toks[1], toks[2])

node, ok := r.Find(key)
if !ok {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/coapcloud/coap-hooks-router
go 1.13

require (
github.com/coapcloud/coap-cli v0.1.1 // indirect
github.com/coapcloud/go-coap v0.1.1
github.com/derekparker/trie v0.0.0-20200317170641-1fdf38b7b0e9
github.com/joho/godotenv v1.3.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/coapcloud/coap-cli v0.1.1 h1:XDFeyz11rpVtVe++1O84Zqf6207kNJ33MlhBnoSlwvQ=
github.com/coapcloud/coap-cli v0.1.1/go.mod h1:UrtA3i0GXKj/acfapH0DJfzFlF7pA+WvzZ4hMARTZKE=
github.com/coapcloud/go-coap v0.0.0-20190923040847-c7ef78cd84df h1:HBYTFGnoYOhg1a365JTZfqIPI49mpSGfR/ib/58lQdQ=
github.com/coapcloud/go-coap v0.0.0-20190923040847-c7ef78cd84df/go.mod h1:mp7wB/7I0+hzZx6QgoUZyjMErTWVv8Kjrwk+ZnNXri8=
github.com/coapcloud/go-coap v0.1.1 h1:xmAFX3d0BcPv/Qjm3jHmeoaUOdocQW2z+/WbBP1ZXNg=
Expand All @@ -13,6 +15,8 @@ github.com/derekparker/trie v0.0.0-20200317170641-1fdf38b7b0e9 h1:G765iDCq7bP5op
github.com/derekparker/trie v0.0.0-20200317170641-1fdf38b7b0e9/go.mod h1:D6ICZm05D9VN1n/8iOtBxLpXtoGp6HDFUJ1RNVieOSE=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dustin/go-coap v0.0.0-20190908170653-752e0f79981e h1:oppjHFVTardH+VyOD32F9uBtgT5Wd/qVqEGcwj389Lc=
github.com/dustin/go-coap v0.0.0-20190908170653-752e0f79981e/go.mod h1:as2rZ2aojRzZF8bGx1bPAn1yi9ICG6LwkiPOj6PBtjc=
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
Expand Down

0 comments on commit 545f002

Please sign in to comment.