aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Black <dblack@twilio.com>2014-01-26 13:42:50 -0800
committerDoug Black <dblack@twilio.com>2014-01-26 13:42:50 -0800
commit5a10edcd157f6a1d16dea18f137ae6f5a87d5001 (patch)
tree39e26745572f62c9a406038281b16879fa76834b
parentfix signature (diff)
downloadsleepy-5a10edcd157f6a1d16dea18f137ae6f5a87d5001.tar.gz
sleepy-5a10edcd157f6a1d16dea18f137ae6f5a87d5001.tar.bz2
sleepy-5a10edcd157f6a1d16dea18f137ae6f5a87d5001.zip
fixes from issues
-rw-r--r--README.md4
-rw-r--r--core.go11
-rw-r--r--http.go15
-rw-r--r--tests/items.go4
-rw-r--r--tests/test.go4
5 files changed, 14 insertions, 24 deletions
diff --git a/README.md b/README.md
index 5447f30..87a9e4a 100644
--- a/README.md
+++ b/README.md
@@ -13,9 +13,7 @@ import (
)
type Item struct {
- sleepy.PostNotSupported
- sleepy.PutNotSupported
- sleepy.DeleteNotSupported
+ sleepy.BaseResource
}
func (item Item) Get(values url.Values) (int, interface{}) {
diff --git a/core.go b/core.go
index c8d7ca9..7f7d0b1 100644
--- a/core.go
+++ b/core.go
@@ -25,6 +25,7 @@ type Api struct{}
func (api *Api) Abort(rw http.ResponseWriter, statusCode int) {
rw.WriteHeader(statusCode)
+ rw.Write([]byte(http.StatusText(statusCode)))
}
func (api *Api) requestHandler(resource Resource) http.HandlerFunc {
@@ -54,12 +55,12 @@ func (api *Api) requestHandler(resource Resource) http.HandlerFunc {
return
}
- responseWriter := json.NewEncoder(rw)
+ content, err := json.Marshal(data)
+ if err != nil {
+ api.Abort(rw, 500)
+ }
rw.WriteHeader(code)
- if responseWriter.Encode(data) != nil {
- api.Abort(rw, 500)
- return
- }
+ rw.Write(content)
}
}
diff --git a/http.go b/http.go
index 7b79aca..8efe657 100644
--- a/http.go
+++ b/http.go
@@ -4,25 +4,20 @@ import (
"net/url"
)
-type (
- GetNotSupported struct{}
- PostNotSupported struct{}
- PutNotSupported struct{}
- DeleteNotSupported struct{}
-)
+type BaseResource struct{}
-func (GetNotSupported) Get(values url.Values) (int, interface{}) {
+func (BaseResource) Get(values url.Values) (int, interface{}) {
return 405, ""
}
-func (PostNotSupported) Post(values url.Values) (int, interface{}) {
+func (BaseResource) Post(values url.Values) (int, interface{}) {
return 405, ""
}
-func (PutNotSupported) Put(values url.Values) (int, interface{}) {
+func (BaseResource) Put(values url.Values) (int, interface{}) {
return 405, ""
}
-func (DeleteNotSupported) Delete(values url.Values) (int, interface{}) {
+func (BaseResource) Delete(values url.Values) (int, interface{}) {
return 405, ""
}
diff --git a/tests/items.go b/tests/items.go
index d39cae9..978b052 100644
--- a/tests/items.go
+++ b/tests/items.go
@@ -6,9 +6,7 @@ import (
)
type Item struct {
- sleepy.PostNotSupported
- sleepy.PutNotSupported
- sleepy.DeleteNotSupported
+ sleepy.BaseResource
}
func (item Item) Get(values url.Values) (int, interface{}) {
diff --git a/tests/test.go b/tests/test.go
index 070d4b6..d3f414c 100644
--- a/tests/test.go
+++ b/tests/test.go
@@ -6,9 +6,7 @@ import (
)
type Bar struct {
- sleepy.PostNotSupported
- sleepy.PutNotSupported
- sleepy.DeleteNotSupported
+ sleepy.BaseResource
}
func (b Bar) Get(values url.Values) (int, interface{}) {
bgstack15