about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFranck Cuny <franck@lumberjaph.net>2013-05-11 15:18:52 -0700
committerFranck Cuny <franck@lumberjaph.net>2013-05-11 15:18:52 -0700
commit1a5bbf79c0088091876fc329aef05baf85d88f8d (patch)
treeae5e337863a33fbf358f0c1d770e5da55b399d37
parentremove the example, it does not make sense anymore (diff)
downloadpath-router-1a5bbf79c0088091876fc329aef05baf85d88f8d.tar.gz
The response object is passed to the matching function.
Instead of letting the matching function creates the response object,
it's created earlier with a default HTTP status of 200, and the object
is passed to the function.
-rw-r--r--route.go6
-rw-r--r--route_test.go5
-rw-r--r--router.go16
-rw-r--r--router_benchmark_test.go5
-rw-r--r--router_test.go5
5 files changed, 17 insertions, 20 deletions
diff --git a/route.go b/route.go
index 2fed631..0aea94c 100644
--- a/route.go
+++ b/route.go
@@ -6,7 +6,7 @@ import (
 	"github.com/franckcuny/web-request"
 )
 
-type fn func(*request.Request) (request.Response, error)
+type fn func(*request.Request, *request.Response) (error)
 type fns map[string]fn
 
 type Route struct {
@@ -108,8 +108,8 @@ L:
 	}
 }
 
-func (self *Route) Execute(request *request.Request) (request.Response, error) {
-	return self.Code(request)
+func (self *Route) Execute(request *request.Request, response *request.Response) error {
+	return self.Code(request, response)
 }
 
 func (self *Route) init() {
diff --git a/route_test.go b/route_test.go
index 8159073..dd38e80 100644
--- a/route_test.go
+++ b/route_test.go
@@ -9,9 +9,8 @@ import (
 	"testing"
 )
 
-func testRoute(req *request.Request) (request.Response, error) {
-	resp := request.Response{}
-	return resp, nil
+func testRoute(req *request.Request, resp *request.Response) error {
+	return nil
 }
 
 func TestBasic(t *testing.T) {
diff --git a/router.go b/router.go
index 85573df..13be84c 100644
--- a/router.go
+++ b/router.go
@@ -33,10 +33,10 @@ func (self *Router) AddOptions() *Router {
 	for _, r := range routes {
 		m := self.GetMethodsForPath(r)
 		allowed := strings.Join(m, ", ")
-		defaultFn := func(req *request.Request) (request.Response, error) {
-			r := request.Response{Status: 204}
-			r.AddHeader("Allow", allowed)
-			return r, nil
+		defaultFn := func(req *request.Request, resp *request.Response) error {
+			resp.Status = 204
+			resp.AddHeader("Allow", allowed)
+			return nil
 		}
 		self.AddRoute(&Route{Method: "OPTIONS", Path: r, Code: defaultFn})
 	}
@@ -54,10 +54,10 @@ func (self *Router) AddNotAllowed() *Router {
 			supportedMethods[m] = true
 		}
 		allowed := strings.Join(methods, ", ")
-		defaultFn := func(req *request.Request) (request.Response, error) {
-			r := request.Response{Status: 405}
-			r.AddHeader("Allow", allowed)
-			return r, nil
+		defaultFn := func(req *request.Request, resp *request.Response) error {
+			resp.Status = 405
+			resp.AddHeader("Allow", allowed)
+			return nil
 		}
 		for _, dm := range defaultHTTPMethods {
 			if supportedMethods[dm] == false {
diff --git a/router_benchmark_test.go b/router_benchmark_test.go
index 42fe579..222b72e 100644
--- a/router_benchmark_test.go
+++ b/router_benchmark_test.go
@@ -8,9 +8,8 @@ import (
 	"testing"
 )
 
-func testSimpleRoute(req *request.Request) (request.Response, error) {
-	resp := request.Response{}
-	return resp, nil
+func testSimpleRoute(req *request.Request, resp *request.Response) error {
+	return nil
 }
 
 func BenchmarkSimple(b *testing.B) {
diff --git a/router_test.go b/router_test.go
index 71fdcaf..167468f 100644
--- a/router_test.go
+++ b/router_test.go
@@ -7,9 +7,8 @@ import (
 	"testing"
 )
 
-func testRouter(req *request.Request) (request.Response, error) {
-	resp := request.Response{}
-	return resp, nil
+func testRouter(req *request.Request, resp *request.Response) error {
+	return nil
 }
 
 func testBasic(t *testing.T) {