diff --git a/router.go b/router.go index dd0a6b9..ab9b2f5 100644 --- a/router.go +++ b/router.go @@ -2,7 +2,6 @@ package router import ( "errors" - "fmt" "net/http" "strings" ) @@ -15,8 +14,7 @@ type route struct { type segment struct { path string - methods map[string]bool - callback http.HandlerFunc + methods map[string]http.HandlerFunc children map[string]*segment } @@ -37,15 +35,19 @@ func (r *Router) AddRoute(method string, path string, callback http.HandlerFunc) if r.lookup == nil { r.lookup = &segment{} r.lookup.children = map[string]*segment{} - r.lookup.methods = map[string]bool{} + r.lookup.methods = map[string]http.HandlerFunc{} + r.lookup.path = "/" } curr := r.lookup - fmt.Printf("Keys: %v", len(keys)) + for i, key := range keys { + if i == 0 { + continue + } - for _, key := range keys { var seg segment + if child, ok := curr.children[key]; !ok { seg = *newSegment(curr.path, key) curr.children[key] = &seg @@ -55,13 +57,12 @@ func (r *Router) AddRoute(method string, path string, callback http.HandlerFunc) } } - if curr.methods[method] { + if _, ok := curr.methods[method]; ok { err = errors.New("path already exists") } if err == nil { - curr.callback = callback - curr.methods[method] = true + curr.methods[method] = callback r.routes = append(r.routes, route{method, path, callback}) } @@ -111,7 +112,7 @@ func newSegment(parentPath string, key string) (seg *segment) { } seg = &segment{} seg.children = map[string]*segment{} - seg.methods = map[string]bool{} + seg.methods = map[string]http.HandlerFunc{} seg.path = path return diff --git a/router_test.go b/router_test.go index bccc688..acf213d 100644 --- a/router_test.go +++ b/router_test.go @@ -28,11 +28,23 @@ func TestAddRouter(t *testing.T) { t.Error("The route was not correctly added to the router: ", err) } + err = addAndCheckRoute(&r, http.MethodDelete, "/items/thing/man/bird/horse/poop", func(http.ResponseWriter, *http.Request) {}, &routeCounter) + + if err != nil { + t.Error("The route was not correctly added to the router: ", err) + } + + err = addAndCheckRoute(&r, http.MethodDelete, "/items/thing/man/bird/cat/poop", func(http.ResponseWriter, *http.Request) {}, &routeCounter) + + if err != nil { + t.Error("The route was not correctly added to the router: ", err) + } + checkLookup(r.lookup) } func checkLookup(curr *segment) { - fmt.Printf("%p { path: %s, methods: %v, children: %v, callback: %p }\n", curr, curr.path, curr.methods, curr.children, curr.callback) + fmt.Printf("%p { path: %s, methods: %v, children: %v}\n", curr, curr.path, curr.methods, curr.children) for _, v := range curr.children { checkLookup(v)