Skip to content

Commit

Permalink
Rename Handler and Route methods to better resemble net/http pkg inte…
Browse files Browse the repository at this point in the history
…rface
  • Loading branch information
mariomenjr committed May 3, 2022
1 parent ae3fd80 commit 71bd694
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
8 changes: 4 additions & 4 deletions handlr.router.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package handlr

// Aliases the RouteFunc method from Handlr.Router to Handlr.
func (h *Handlr) RouteFunc(path string, routeHandler RouteHandler) {
func (h *Handlr) RouteFunc(path string, routeHandler RouteHandlerFunc) {
h.router.RouteFunc(path, routeHandler)
}

// Aliases the HandlerFunc method from Handlr.Router to Handlr.
func (h *Handlr) HandlerFunc(path string, actionHandler ActionHandler) {
h.router.HandlerFunc(path, actionHandler)
// Aliases the HandleFunc method from Handlr.Router to Handlr.
func (h *Handlr) HandleFunc(path string, actionHandler ActionHandlerFunc) {
h.router.HandleFunc(path, actionHandler)
}
22 changes: 15 additions & 7 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,32 @@ type Router struct {
path string
parent *Router
children []*Router
handler *ActionHandler
handler *ActionHandlerFunc
}

func (rt *Router) Route(path string, routeHandler RouteHandler) {
rt.RouteFunc(path, routeHandler.RouteHTTP)
}

// Allows RouteFunc registration.
// You don't program behavior through this method.
func (rt *Router) RouteFunc(path string, routeHandler RouteHandler) {
func (rt *Router) RouteFunc(path string, routeHandlerFunc RouteHandlerFunc) {
router := &Router{path: path, parent: rt}
routeHandler(router)
routeHandlerFunc(router)

rt.children = append(rt.children, router)
}

func (rt *Router) Handle(path string, actionHandler http.Handler) {
rt.HandleFunc(path, actionHandler.ServeHTTP)
}

// Allows HandlerFunc registration which gives you the ability
// to tie a behavior to a path.
// i.e. Get a record from database by hiting URL:
// http://example.org/get/record?id=1
func (rt *Router) HandlerFunc(path string, actionHandler ActionHandler) {
router := &Router{path: path, parent: rt, handler: &actionHandler}
// http://example.org/get/record/1
func (rt *Router) HandleFunc(path string, actionHandlerFunc ActionHandlerFunc) {
router := &Router{path: path, parent: rt, handler: &actionHandlerFunc}

rt.children = append(rt.children, router)
}
Expand All @@ -55,7 +63,7 @@ func (rt *Router) buildPath() string {
}

// Recursively find a handler to the request
func (rt *Router) findHandler(r *http.Request) *ActionHandler {
func (rt *Router) findHandler(r *http.Request) *ActionHandlerFunc {
for _, v := range rt.children {
if v.handler != nil && v.isMatch(r) {
return v.handler
Expand Down
12 changes: 6 additions & 6 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type unitTests struct {
w *httptest.ResponseRecorder
m string
rt func()
fh func() *ActionHandler
fh func() *ActionHandlerFunc
}

func TestRegisterRoute(t *testing.T) {
Expand All @@ -28,7 +28,7 @@ func TestRegisterHandler(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()

h.HandlerFunc("/", func(w http.ResponseWriter, r *http.Request) {
h.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
t.Logf("Handler for %s registered correctly.", r.URL.Path)
})

Expand Down Expand Up @@ -73,13 +73,13 @@ func TestFindHandler(t *testing.T) {
rt: func() {
h.RouteFunc("/", func(r1 *Router) {
r1.RouteFunc("/a", func(r2 *Router) {
r2.HandlerFunc("/b", func(w http.ResponseWriter, r *http.Request) {
r2.HandleFunc("/b", func(w http.ResponseWriter, r *http.Request) {
t.Logf("Handler for %s matched correctly.", r.URL.Path)
})
})
})
},
fh: func() *ActionHandler {
fh: func() *ActionHandlerFunc {
e := len(h.router.children) - 1 // For /
a := len(h.router.children[e].children) - 1 // For /a
b := len(h.router.children[e].children[a].children) - 1 // For /b
Expand All @@ -92,11 +92,11 @@ func TestFindHandler(t *testing.T) {
w: httptest.NewRecorder(),
m: "Handler matched correctly.",
rt: func() {
h.HandlerFunc("/x/y/z", func(w http.ResponseWriter, r *http.Request) {
h.HandleFunc("/x/y/z", func(w http.ResponseWriter, r *http.Request) {
t.Logf("Handler for %s matched correctly.", r.URL.Path)
})
},
fh: func() *ActionHandler {
fh: func() *ActionHandlerFunc {
return h.router.children[1].handler
},
},
Expand Down
8 changes: 6 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ package handlr

import "net/http"

type ActionHandler func(w http.ResponseWriter, r *http.Request)
type RouteHandler func(r *Router)
type ActionHandlerFunc func(w http.ResponseWriter, r *http.Request)
type RouteHandlerFunc func(r *Router)

type RouteHandler interface {
RouteHTTP(r *Router)
}

0 comments on commit 71bd694

Please sign in to comment.