Browse Source

Added Router.Function for callback routes

master
Gisle Aune 7 years ago
parent
commit
440db55927
  1. 18
      function.go
  2. 42
      function_test.go
  3. 16
      resource.go
  4. 6
      router.go

18
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)
}

42
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")
}
}

16
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}
}

6
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),

Loading…
Cancel
Save