diff options
Diffstat (limited to 'core.go')
-rw-r--r-- | core.go | 24 |
1 files changed, 10 insertions, 14 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 + } } } |