about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFranck Cuny <franck@lumberjaph.net>2013-04-22 19:12:22 -0700
committerFranck Cuny <franck@lumberjaph.net>2013-04-22 19:12:22 -0700
commit589c2d57ef9d181783c0ff1b6dd0dd49983dceb6 (patch)
tree8a32415cfaabd81b4d2d5eaed1023a81da56ca1e
parentUpdate and rename README.org to README.md (diff)
downloadpath-router-589c2d57ef9d181783c0ff1b6dd0dd49983dceb6.tar.gz
Split only once per request the path in components.
There's no need to split the URL in components every time we call the
Match function on a Route.  We can split it once in the dispatcher and
then path the array to the function.

Sadly, this makes the interface a little bit weaker, since we have to
pass the HTTP method and the list instead of the full HTTP Request.  I
think in the future we should pass our own HTTP request object, with the
split already in done in this one.
-rw-r--r--dispatcher.go6
-rw-r--r--route.go10
2 files changed, 9 insertions, 7 deletions
diff --git a/dispatcher.go b/dispatcher.go
index a50f2e8..62dee10 100644
--- a/dispatcher.go
+++ b/dispatcher.go
@@ -4,6 +4,7 @@ import (
 	"errors"
 	"fmt"
 	"net/http"
+	"strings"
 )
 
 type Dispatcher struct {
@@ -43,8 +44,11 @@ func (self *Dispatcher) Match(request *http.Request) (*Match, error) {
 
 	matches := []*Match{}
 
+	method := request.Method
+	components := strings.Split(request.URL.Path, "/")
+
 	for _, r := range self.Routes {
-		match := r.Match(request)
+		match := r.Match(method, components)
 		if match != nil {
 			matches = append(matches, match)
 		}
diff --git a/route.go b/route.go
index 832313b..46998c1 100644
--- a/route.go
+++ b/route.go
@@ -1,7 +1,6 @@
 package mooh
 
 import (
-	"net/http"
 	"regexp"
 	"strings"
 )
@@ -36,10 +35,10 @@ func (self *Route) convertComponentName(name string) string {
 	return newName[1]
 }
 
-func (self *Route) Match(request *http.Request) *Match {
+func (self *Route) Match(method string, components []string) *Match {
 	methodMatch := false
 	for m, _ := range self.Executors {
-		if m == request.Method {
+		if m == method {
 			methodMatch = true
 		}
 	}
@@ -47,7 +46,6 @@ func (self *Route) Match(request *http.Request) *Match {
 		return nil
 	}
 
-	components := strings.Split(request.URL.Path, "/")
 	if len(components) < self.LengthWithoutOptional || len(components) > self.Length {
 		return nil
 	}
@@ -70,9 +68,9 @@ func (self *Route) Match(request *http.Request) *Match {
 	}
 
 	return &Match{
-		Path:    request.URL.Path,
+		Path:    strings.Join(components, "/"),
 		Route:   *self,
-		Method:  request.Method,
+		Method:  method,
 		Mapping: mapping,
 	}
 }