diff options
-rw-r--r-- | dispatcher.go | 11 | ||||
-rw-r--r-- | mooh.go | 16 | ||||
-rw-r--r-- | request.go | 5 | ||||
-rw-r--r-- | route.go | 7 | ||||
-rw-r--r-- | router_test.go | 16 |
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() |