From 440db5592733ab6a8417b03aec66435dc9184fd3 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Sun, 13 Aug 2017 17:30:40 +0200 Subject: [PATCH] Added Router.Function for callback routes --- function.go | 18 ++++++++++++++++++ function_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ resource.go | 16 ++++++++-------- router.go | 6 +++++- 4 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 function.go create mode 100644 function_test.go diff --git a/function.go b/function.go new file mode 100644 index 0000000..a6c894e --- /dev/null +++ b/function.go @@ -0,0 +1,18 @@ +package wrouter + +import ( + "net/http" + + "git.aiterp.net/gisle/wrouter/auth" +) + +// FunctionHandlerFunc is simply a function that is called directly rather than through a struct +type FunctionHandlerFunc func(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool + +type functionHandler struct { + function FunctionHandlerFunc +} + +func (handler *functionHandler) Handle(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool { + return handler.function(path, w, req, user) +} diff --git a/function_test.go b/function_test.go new file mode 100644 index 0000000..8d2585d --- /dev/null +++ b/function_test.go @@ -0,0 +1,42 @@ +package wrouter + +import ( + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" + + "git.aiterp.net/gisle/wrouter/response" + + "git.aiterp.net/gisle/wrouter/auth" +) + +func TestFunction(t *testing.T) { + router := &Router{} + router.Function("/test", func(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool { + response.Text(w, 200, path+" called") + return true + }) + server := httptest.NewServer(router) + + resp, err := http.Get(server.URL + "/test/24g42g24f24f24f24f") + if err != nil { + t.Error("Request:", err) + t.Fail() + } + + if resp.StatusCode != 200 { + t.Error("Expected 200, got", resp.Status) + t.Fail() + } + + if resp.ContentLength == 0 { + t.Error("No content returned from server") + t.Fail() + } + + data, _ := ioutil.ReadAll(resp.Body) + if string(data) != "/test called" { + t.Errorf(`Body: "%s" != "%s"`, string(data), "/test called") + } +} diff --git a/resource.go b/resource.go index 257575d..babcaa6 100644 --- a/resource.go +++ b/resource.go @@ -9,21 +9,21 @@ import ( "git.aiterp.net/gisle/wrouter/auth" ) -type Func func(http.ResponseWriter, *http.Request, *auth.User) -type IDFunc func(http.ResponseWriter, *http.Request, string, *auth.User) +type ResourceFunc func(http.ResponseWriter, *http.Request, *auth.User) +type ResourceIDFunc func(http.ResponseWriter, *http.Request, string, *auth.User) type Resource struct { - list Func - create Func - get IDFunc - update IDFunc - delete IDFunc + list ResourceFunc + create ResourceFunc + get ResourceIDFunc + update ResourceIDFunc + delete ResourceIDFunc PrivateRead bool PrivateWrite bool } -func NewResource(list, create Func, get, update, delete IDFunc) *Resource { +func NewResource(list, create ResourceFunc, get, update, delete ResourceIDFunc) *Resource { return &Resource{list, create, get, update, delete, false, false} } diff --git a/router.go b/router.go index 2c045e4..6ceb55d 100644 --- a/router.go +++ b/router.go @@ -75,7 +75,7 @@ func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { w.Write([]byte("Not Found: " + req.URL.Path)) } -func (router *Router) Resource(mount string, list, create Func, get, update, delete IDFunc) { +func (router *Router) Resource(mount string, list, create ResourceFunc, get, update, delete ResourceIDFunc) { router.Route(mount, NewResource(list, create, get, update, delete)) } @@ -83,6 +83,10 @@ func (router *Router) Static(mount string, filePath string) { router.Route(mount, NewStatic(filePath)) } +func (router *Router) Function(mount string, function FunctionHandlerFunc) { + router.Route(mount, &functionHandler{function}) +} + func (router *Router) Listen(host string, port int) (*http.Server, error) { srv := &http.Server{ Addr: fmt.Sprintf("%s:%d", host, port),