From db7199e830cd8911b1bbbf416c2cf7f70decffbb Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Mon, 6 May 2013 20:42:15 -0700 Subject: Remove code not related to the router. `foom` is now the web framework, so the code from mooh.go has been moved to that repository. The response and request are handled by the new `web-request` project, and so the code has been moved to this repository. --- mooh.go | 32 -------------------------------- request.go | 21 --------------------- request_test.go | 41 ----------------------------------------- response.go | 10 ---------- route.go | 5 +++-- router.go | 9 +++++---- 6 files changed, 8 insertions(+), 110 deletions(-) delete mode 100644 mooh.go delete mode 100644 request.go delete mode 100644 request_test.go delete mode 100644 response.go diff --git a/mooh.go b/mooh.go deleted file mode 100644 index 3733b58..0000000 --- a/mooh.go +++ /dev/null @@ -1,32 +0,0 @@ -package mooh - -import ( - "fmt" - "net/http" -) - -func (self *Router) ServeHTTP(resp http.ResponseWriter, req *http.Request) { - - match, err := self.Match(req) - - if err != nil { - fmt.Println(err) - fmt.Printf("oups") - return - } - - if match == nil { - fmt.Fprint(resp, "Not Found") - return - } - - request := NewRequest(req, match) - - nresp, error := match.Route.Execute(request) - - if error == nil { - fmt.Fprint(resp, nresp.Content) - } else { - fmt.Println(error) - } -} diff --git a/request.go b/request.go deleted file mode 100644 index 715ccac..0000000 --- a/request.go +++ /dev/null @@ -1,21 +0,0 @@ -package mooh - -import ( - "net/http" -) - -type Request struct { - routeParams map[string]string - *http.Request -} - -func (self *Request) RouteParam(name string) string { - return self.routeParams[name] -} - -func NewRequest(req *http.Request, match *Match) *Request { - return &Request{ - match.Mapping, - req, - } -} diff --git a/request_test.go b/request_test.go deleted file mode 100644 index d112087..0000000 --- a/request_test.go +++ /dev/null @@ -1,41 +0,0 @@ -package mooh - -import ( - "net/http" - "net/url" - "testing" - "regexp" -) - -func testRequestRoute(req *Request) (Response, error) { - resp := Response{} - return resp, nil -} - -func buildRouter() *Router { - router := BuildRouter() - router.AddRoute(&Route{ - Method: "GET", - Path: "/foo/{bar}", - Code: testRequestRoute, - Validations: map[string]*regexp.Regexp{ - "bar": regexp.MustCompile("\\w+"), - }, - }) - return router -} - -func TestBasicRequest(t *testing.T) { - router := buildRouter() - t.Log(router.routes[0].components) - r := &http.Request{Method: "GET", URL: &url.URL{Path: "/foo/bar"}} - m, _ := router.Match(r) - request := NewRequest(r, m) - - if request == nil { - t.Fatal() - } - if p := request.RouteParam("bar"); p != "bar" { - t.Fatal() - } -} diff --git a/response.go b/response.go deleted file mode 100644 index e71b546..0000000 --- a/response.go +++ /dev/null @@ -1,10 +0,0 @@ -package mooh - -type Response struct { - Headers map[string][]string - Content string - Status int -} - -func (self *Response) AddHeader(headerName string, headerValue string) { -} diff --git a/route.go b/route.go index 925bfb5..98e61a6 100644 --- a/route.go +++ b/route.go @@ -3,9 +3,10 @@ package mooh import ( "regexp" "strings" + "github.com/franckcuny/web-request" ) -type fn func(*Request) (Response, error) +type fn func(*request.Request) (request.Response, error) type fns map[string]fn type Route struct { @@ -107,7 +108,7 @@ L: } } -func (self *Route) Execute(request *Request) (Response, error) { +func (self *Route) Execute(request *request.Request) (request.Response, error) { return self.Code(request) } diff --git a/router.go b/router.go index 4eab24e..8f2ef9e 100644 --- a/router.go +++ b/router.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "strings" + "github.com/franckcuny/web-request" ) type Router struct { @@ -30,8 +31,8 @@ func (self *Router) AddOptions() *Router { for _, r := range routes { m := self.GetMethodsForPath(r) allowed := strings.Join(m, ", ") - defaultFn := func(req *Request) (Response, error) { - r := Response{Status: 204} + defaultFn := func(req *request.Request) (request.Response, error) { + r := request.Response{Status: 204} r.AddHeader("Allow", allowed) return r, nil } @@ -51,8 +52,8 @@ func (self *Router) AddNotAllowed() *Router { supportedMethods[m] = true } allowed := strings.Join(methods, ", ") - defaultFn := func(req *Request) (Response, error) { - r := Response{Status: 405} + defaultFn := func(req *request.Request) (request.Response, error) { + r := request.Response{Status: 405} r.AddHeader("Allow", allowed) return r, nil } -- cgit 1.4.1