From d47df4de58172593c56513d37d2b75d4af578e58 Mon Sep 17 00:00:00 2001 From: Alex Lobunets Date: Fri, 7 Feb 2014 10:02:41 +0100 Subject: Added request headers as Get/Put/Post/Delete argument. Added response headers map as 3rd argument of the return from Get/Put/Post/Delete handlers. Thus, resource can access request headers and set custom response headers if required --- README.md | 4 ++-- core.go | 17 +++++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 5794d22..a3cd587 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,10 @@ import ( type Item struct { } -func (item Item) Get(values url.Values) (int, interface{}) { +func (item Item) Get(values url.Values, headers map[string][]string) (int, interface{}, map[string][]string) { items := []string{"item1", "item2"} data := map[string][]string{"items": items} - return 200, data + return 200, data, map[string][]string{"Content-type": "application/json"} } func main() { diff --git a/core.go b/core.go index 0ef5590..6023014 100644 --- a/core.go +++ b/core.go @@ -18,25 +18,25 @@ const ( // GetSupported is the interface that provides the Get // method a resource must support to receive HTTP GETs. type GetSupported interface { - Get(url.Values) (int, interface{}) + Get(url.Values, map[string][]string) (int, interface{}, map[string][]string) } // PostSupported is the interface that provides the Post // method a resource must support to receive HTTP POSTs. type PostSupported interface { - Post(url.Values) (int, interface{}) + Post(url.Values, map[string][]string) (int, interface{}, map[string][]string) } // PutSupported is the interface that provides the Put // method a resource must support to receive HTTP PUTs. type PutSupported interface { - Put(url.Values) (int, interface{}) + Put(url.Values, map[string][]string) (int, interface{}, map[string][]string) } // DeleteSupported is the interface that provides the Delete // method a resource must support to receive HTTP DELETEs. type DeleteSupported interface { - Delete(url.Values) (int, interface{}) + Delete(url.Values, map[string][]string) (int, interface{}, map[string][]string) } // An API manages a group of resources by routing requests @@ -62,7 +62,7 @@ func (api *API) requestHandler(resource interface{}) http.HandlerFunc { return } - var handler func(url.Values) (int, interface{}) + var handler func(url.Values, map[string][]string) (int, interface{}, map[string][]string) switch request.Method { case GET: @@ -88,13 +88,18 @@ func (api *API) requestHandler(resource interface{}) http.HandlerFunc { return } - code, data := handler(request.Form) + code, data, headers := handler(request.Form, request.Header) content, err := json.Marshal(data) if err != nil { rw.WriteHeader(http.StatusInternalServerError) return } + for name, values := range headers { + for _, value := range values { + rw.Header().Add(name, value) + } + } rw.WriteHeader(code) rw.Write(content) } -- cgit From 22611424746fe906dbf8ecfc402e29833b60415d Mon Sep 17 00:00:00 2001 From: Alex Lobunets Date: Fri, 7 Feb 2014 10:09:23 +0100 Subject: Updated type in the README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a3cd587..4c5ceb7 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ type Item struct { } func (item Item) Get(values url.Values, headers map[string][]string) (int, interface{}, map[string][]string) { items := []string{"item1", "item2"} data := map[string][]string{"items": items} - return 200, data, map[string][]string{"Content-type": "application/json"} + return 200, data, map[string][]string{"Content-type": {"application/json"}} } func main() { -- cgit