about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFranck Cuny <franck@lumberjaph.net>2013-04-21 16:00:22 -0700
committerFranck Cuny <franck@lumberjaph.net>2013-04-21 16:00:22 -0700
commit236f53dfceb995b528e43f0e0b138e22a9c3b0a6 (patch)
tree77c238b8d9de49b9f6d56e49a16464a3cf476fc5
parentRename the type fn to fns and add a real type fn (diff)
downloadpath-router-236f53dfceb995b528e43f0e0b138e22a9c3b0a6.tar.gz
Simplify the dispatch's workflow.
Forward the http.Request object to the Dispatcher's Match function, and
instead of returning the route that match, return the whole Match
object.

We create our own Request object later in the process.
-rw-r--r--dispatcher.go11
-rw-r--r--mooh.go16
-rw-r--r--request.go5
-rw-r--r--route.go7
-rw-r--r--router_test.go16
5 files changed, 31 insertions, 24 deletions
diff --git a/dispatcher.go b/dispatcher.go
index 78c377f..a50f2e8 100644
--- a/dispatcher.go
+++ b/dispatcher.go
@@ -3,6 +3,7 @@ package mooh
 import (
 	"errors"
 	"fmt"
+	"net/http"
 )
 
 type Dispatcher struct {
@@ -38,7 +39,7 @@ func (self *Dispatcher) AddRoute(path string, method string, code func(*Request)
 	}
 }
 
-func (self *Dispatcher) Match(request Request) (*Route, error) {
+func (self *Dispatcher) Match(request *http.Request) (*Match, error) {
 
 	matches := []*Match{}
 
@@ -52,14 +53,14 @@ func (self *Dispatcher) Match(request Request) (*Route, error) {
 	if len(matches) == 0 {
 		return nil, nil
 	} else if len(matches) == 1 {
-		return &matches[0].Route, nil
+		return matches[0], nil
 	} else {
-		return self.disambiguateMatches(request.Request.URL.Path, matches)
+		return self.disambiguateMatches(request.URL.Path, matches)
 	}
 	return nil, nil
 }
 
-func (self *Dispatcher) disambiguateMatches(path string, matches []*Match) (*Route, error) {
+func (self *Dispatcher) disambiguateMatches(path string, matches []*Match) (*Match, error) {
 	min := -1
 	found := []*Match{}
 
@@ -82,7 +83,7 @@ func (self *Dispatcher) disambiguateMatches(path string, matches []*Match) (*Rou
 		err := errors.New(msg)
 		return nil, err
 	}
-	return &found[0].Route, nil
+	return found[0], nil
 }
 
 // func (*dispatcher) ListRoutes() {
diff --git a/mooh.go b/mooh.go
index dcee3c9..465caf5 100644
--- a/mooh.go
+++ b/mooh.go
@@ -7,22 +7,24 @@ import (
 
 func (self *Dispatcher) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
 
-	request := Request{
-		req,
-	}
-
-	route, err := self.Match(request)
+	match, err := self.Match(req)
 
 	if err != nil {
 		fmt.Printf("oups")
 		return
 	}
-	if route == nil {
+
+	if match == nil {
 		fmt.Fprint(resp, "Not Found")
 		return
 	}
 
-	nresp, error := route.Execute(&request)
+	request := Request{
+		match.Mapping,
+		req,
+	}
+
+	nresp, error := match.Route.Execute(&request)
 
 	if error == nil {
 		fmt.Fprint(resp, nresp.Content)
diff --git a/request.go b/request.go
index eee6c80..02ad2ba 100644
--- a/request.go
+++ b/request.go
@@ -5,5 +5,10 @@ import (
 )
 
 type Request struct {
+	params map[string]string
 	*http.Request
 }
+
+func (self *Request) Param(name string) string {
+	return self.params[name]
+}
diff --git a/route.go b/route.go
index 768315e..f17fe7b 100644
--- a/route.go
+++ b/route.go
@@ -3,6 +3,7 @@ package mooh
 import (
 	"regexp"
 	"strings"
+	"net/http"
 )
 
 type fn func(*Request) (Response, error)
@@ -29,7 +30,7 @@ var componentIsVariable = regexp.MustCompile("^:")
 var componentsIsOptional = regexp.MustCompile("^?:")
 var namedComponentsRegex = regexp.MustCompile("^:(.*)$")
 
-func (self *Route) Match(request Request) *Match {
+func (self *Route) Match(request *http.Request) *Match {
 	methodMatch := false
 	for m, _ := range self.Executors {
 		if m == request.Method {
@@ -40,7 +41,7 @@ func (self *Route) Match(request Request) *Match {
 		return nil
 	}
 
-	components := strings.Split(request.Request.URL.Path, "/")
+	components := strings.Split(request.URL.Path, "/")
 	if len(components) < self.LengthWithoutOptional || len(components) > self.Length {
 		return nil
 	}
@@ -63,7 +64,7 @@ func (self *Route) Match(request Request) *Match {
 	}
 
 	return &Match{
-		Path:    request.Request.URL.Path,
+		Path:    request.URL.Path,
 		Route:   *self,
 		Method:  request.Method,
 		Mapping: mapping,
diff --git a/router_test.go b/router_test.go
index 95145b1..6003c28 100644
--- a/router_test.go
+++ b/router_test.go
@@ -41,9 +41,8 @@ func TestMatch(t *testing.T) {
 		url.URL{Path: "/blog/2013/21/4"},
 	}
 	for _, p := range pathToTests {
-		r := http.Request{URL: &p, Method: "GET"}
-		req := Request{&r}
-		m, _ := router.Match(req)
+		r := &http.Request{URL: &p, Method: "GET"}
+		m, _ := router.Match(r)
 		if m == nil {
 			t.Fatal()
 		}else{
@@ -64,9 +63,8 @@ func TestMatchOptional(t *testing.T) {
 		url.URL{Path: "/blog/2013/21"},
 	}
 	for _, p := range pathToTests {
-		r := http.Request{URL: &p, Method: "GET"}
-		req := Request{&r}
-		m, _ := router.Match(req)
+		r := &http.Request{URL: &p, Method: "GET"}
+		m, _ := router.Match(r)
 		if m == nil {
 			t.Fatal()
 		}else{
@@ -80,12 +78,12 @@ func TestAmbigiousSimple(t *testing.T) {
 	router.AddRoute("/foo/bar", "GET", testRoute)
 	router.AddRoute("/foo/:bar", "GET", testRoute)
 
-	r := Request{&http.Request{Method: "GET", URL: &url.URL{Path: "/foo/bar"}}}
+	r := &http.Request{Method: "GET", URL: &url.URL{Path: "/foo/bar"}}
 	m, _ := router.Match(r)
 	if m == nil {
 		t.Fatal()
 	}else{
-		fmt.Println(fmt.Sprintf("%s match for %s", r.Request.URL.Path, m.Path))
+		fmt.Println(fmt.Sprintf("%s match for %s", r.URL.Path, m.Path))
 	}
 }
 
@@ -94,7 +92,7 @@ func TestAmbigiousFail(t *testing.T) {
 	router.AddRoute("/foo/:bar", "GET", testRoute)
 	router.AddRoute("/:foo/bar", "GET", testRoute)
 
-	r := Request{&http.Request{Method: "GET", URL: &url.URL{Path: "/foo/bar"}}}
+	r := &http.Request{Method: "GET", URL: &url.URL{Path: "/foo/bar"}}
 	m, err := router.Match(r)
 	if m != nil {
 		t.Fatal()