diff options
-rw-r--r-- | core.go | 24 | ||||
-rw-r--r-- | http.go | 8 | ||||
-rw-r--r-- | tests/items.go | 2 | ||||
-rw-r--r-- | tests/test.go | 2 |
4 files changed, 16 insertions, 20 deletions
@@ -15,10 +15,10 @@ const ( ) type Resource interface { - Get(values ...url.Values) (int, interface{}) - Post(values ...url.Values) (int, interface{}) - Put(values ...url.Values) (int, interface{}) - Delete(values ...url.Values) (int, interface{}) + Get(values url.Values) (int, interface{}) + Post(values url.Values) (int, interface{}) + Put(values url.Values) (int, interface{}) + Delete(values url.Values) (int, interface{}) } type Api struct{} @@ -27,9 +27,7 @@ func (api *Api) Abort(rw http.ResponseWriter, statusCode int) { rw.WriteHeader(statusCode) } -type HandleFunc func(http.ResponseWriter, *http.Request) - -func (api *Api) requestHandler(resource Resource) HandleFunc { +func (api *Api) requestHandler(resource Resource) http.HandlerFunc { return func(rw http.ResponseWriter, request *http.Request) { var data interface{} @@ -56,14 +54,12 @@ func (api *Api) requestHandler(resource Resource) HandleFunc { return } - content, err := json.Marshal(data) - if err != nil { - api.Abort(rw, 500) - return - } - + responseWriter := json.NewEncoder(rw) rw.WriteHeader(code) - rw.Write(content) + if responseWriter.Encode(data) != nil { + api.Abort(rw, 500) + return + } } } @@ -11,18 +11,18 @@ type ( DeleteNotSupported struct{} ) -func (GetNotSupported) Get(values ...url.Values) (int, interface{}) { +func (GetNotSupported) Get(values url.Values) (int, interface{}) { return 405, "" } -func (PostNotSupported) Post(values ...url.Values) (int, interface{}) { +func (PostNotSupported) Post(values url.Values) (int, interface{}) { return 405, "" } -func (PutNotSupported) Put(values ...url.Values) (int, interface{}) { +func (PutNotSupported) Put(values url.Values) (int, interface{}) { return 405, "" } -func (DeleteNotSupported) Delete(values ...url.Values) (int, interface{}) { +func (DeleteNotSupported) Delete(values url.Values) (int, interface{}) { return 405, "" } diff --git a/tests/items.go b/tests/items.go index ee86c3e..d39cae9 100644 --- a/tests/items.go +++ b/tests/items.go @@ -11,7 +11,7 @@ type Item struct { sleepy.DeleteNotSupported } -func (item Item) Get(values ...url.Values) (int, interface{}) { +func (item Item) Get(values url.Values) (int, interface{}) { items := []string{"item1", "item2"} data := map[string][]string{"items": items} diff --git a/tests/test.go b/tests/test.go index d225072..070d4b6 100644 --- a/tests/test.go +++ b/tests/test.go @@ -11,7 +11,7 @@ type Bar struct { sleepy.DeleteNotSupported } -func (b Bar) Get(values ...url.Values) (int, interface{}) { +func (b Bar) Get(values url.Values) (int, interface{}) { return 200, map[string]string{"hello": "goodbye"} } |