about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFranck Cuny <franck@lumberjaph.net>2013-04-21 11:06:13 -0700
committerFranck Cuny <franck@lumberjaph.net>2013-04-21 11:06:13 -0700
commit46d36782b74ccc52ba3095ab2f0db0162c3b8670 (patch)
tree18671ba05932f2f69d8d1cdcb945514096a39dcf
parentSupport parameters in URL. (diff)
downloadpath-router-46d36782b74ccc52ba3095ab2f0db0162c3b8670.tar.gz
Clean the Path from useless /.
If a path starts with /, remove it.
-rw-r--r--dispatcher.go10
-rw-r--r--route.go8
2 files changed, 17 insertions, 1 deletions
diff --git a/dispatcher.go b/dispatcher.go
index 987f182..8948fad 100644
--- a/dispatcher.go
+++ b/dispatcher.go
@@ -1,5 +1,9 @@
 package mooh
 
+import (
+	"strings"
+)
+
 type Dispatcher struct {
 	Routes      []Route
 	RouteAccess map[string]*Route
@@ -34,6 +38,12 @@ func (self *Dispatcher) AddRoute(path string, method string, code func(*Request)
 }
 
 func (self *Dispatcher) Match(request Request) *Route {
+
+	// If the path starts with /, remove it
+	if request.Request.URL.Path[0] == '/' {
+		request.Request.URL.Path = strings.TrimLeft(request.Request.URL.Path, "/")
+	}
+
 	for _, r := range self.Routes {
 		match := r.Match(request)
 		if match != nil {
diff --git a/route.go b/route.go
index 9996f5c..3ba3548 100644
--- a/route.go
+++ b/route.go
@@ -75,7 +75,13 @@ func (self *Route) Execute(request *Request) (Response, error) {
 
 func MakeRoute(path string, method string, code func(*Request) (Response, error)) Route {
 
-	components := strings.Split(path, "/")
+	components := []string{}
+
+	for _, c := range strings.Split(path, "/") {
+		if c != "" {
+			components = append(components, c)
+		}
+	}
 
 	namedComponents := getNamedComponents(components)
 	exec := fn{method: code}