about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFranck Cuny <franck@lumberjaph.net>2013-04-22 19:25:05 -0700
committerFranck Cuny <franck@lumberjaph.net>2013-04-22 19:25:05 -0700
commit4b0c510385a1f9b96006a8ad222b2b898c919deb (patch)
treecaff08557ab02ccf1e5e9b3707cc8f9fb575e27d
parentSplit only once per request the path in components. (diff)
downloadpath-router-4b0c510385a1f9b96006a8ad222b2b898c919deb.tar.gz
In each route, keep a map of HTTP methods available.
By keeping a map of the HTTP methods available for a given route, we
don't have to iter over a list of executors when we try to find if the
request match, we can quickly check if the methos is set to true.
-rw-r--r--dispatcher.go1
-rw-r--r--route.go11
2 files changed, 5 insertions, 7 deletions
diff --git a/dispatcher.go b/dispatcher.go
index 62dee10..3b98b5f 100644
--- a/dispatcher.go
+++ b/dispatcher.go
@@ -37,6 +37,7 @@ func (self *Dispatcher) AddRoute(path string, method string, code func(*Request)
 		self.RouteAccess[path] = &route
 	} else {
 		r.Executors[method] = code
+		r.Methods[method] = true
 	}
 }
 
diff --git a/route.go b/route.go
index 46998c1..9fc86ee 100644
--- a/route.go
+++ b/route.go
@@ -16,6 +16,7 @@ type Route struct {
 	OptionalNamedComponents map[string]bool
 	Length                  int
 	LengthWithoutOptional   int
+	Methods                 map[string]bool
 }
 
 type Match struct {
@@ -36,13 +37,8 @@ func (self *Route) convertComponentName(name string) string {
 }
 
 func (self *Route) Match(method string, components []string) *Match {
-	methodMatch := false
-	for m, _ := range self.Executors {
-		if m == method {
-			methodMatch = true
-		}
-	}
-	if methodMatch == false {
+
+	if self.Methods[method] != true {
 		return nil
 	}
 
@@ -99,6 +95,7 @@ func MakeRoute(path string, method string, code fn) Route {
 		OptionalNamedComponents: optComponents,
 		Length:                  len(components),
 		LengthWithoutOptional:   len(components) - len(optComponents),
+		Methods:                 map[string]bool{method: true},
 	}
 
 	return route