Add http.Handler adapter
diff --git a/router.go b/router.go
index 13cec28..c6614b2 100644
--- a/router.go
+++ b/router.go
@@ -205,6 +205,16 @@
root.addRoute(path, handle)
}
+// Handler is an adapter which allows the usage of an http.Handler as a
+// request handle.
+func (r *Router) Handler(method, path string, handler http.Handler) {
+ r.Handle(method, path,
+ func(w http.ResponseWriter, req *http.Request, _ Params) {
+ handler.ServeHTTP(w, req)
+ },
+ )
+}
+
// HandlerFunc is an adapter which allows the usage of an http.HandlerFunc as a
// request handle.
func (r *Router) HandlerFunc(method, path string, handler http.HandlerFunc) {
diff --git a/router_test.go b/router_test.go
index e3acc2f..f13d598 100644
--- a/router_test.go
+++ b/router_test.go
@@ -67,8 +67,18 @@
}
}
+type handlerStruct struct {
+ handeled *bool
+}
+
+func (h handlerStruct) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ *h.handeled = true
+}
+
func TestRouterAPI(t *testing.T) {
- var get, post, put, patch, delete, handlerFunc bool
+ var get, post, put, patch, delete, handler, handlerFunc bool
+
+ httpHandler := handlerStruct{&handler}
router := New()
router.GET("/GET", func(w http.ResponseWriter, r *http.Request, _ Params) {
@@ -86,6 +96,7 @@
router.DELETE("/DELETE", func(w http.ResponseWriter, r *http.Request, _ Params) {
delete = true
})
+ router.Handler("GET", "/Handler", httpHandler)
router.HandlerFunc("GET", "/HandlerFunc", func(w http.ResponseWriter, r *http.Request) {
handlerFunc = true
})
@@ -122,6 +133,12 @@
t.Error("routing DELETE failed")
}
+ r, _ = http.NewRequest("GET", "/Handler", nil)
+ router.ServeHTTP(w, r)
+ if !handler {
+ t.Error("routing Handler failed")
+ }
+
r, _ = http.NewRequest("GET", "/HandlerFunc", nil)
router.ServeHTTP(w, r)
if !handlerFunc {