|
|
@ -35,7 +35,6 @@ import ( |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"github.com/gorilla/mux" |
|
|
|
"github.com/gorilla/mux" |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
|
"net/url" |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
const ( |
|
|
@ -43,30 +42,44 @@ const ( |
|
|
|
POST = "POST" |
|
|
|
POST = "POST" |
|
|
|
PUT = "PUT" |
|
|
|
PUT = "PUT" |
|
|
|
DELETE = "DELETE" |
|
|
|
DELETE = "DELETE" |
|
|
|
|
|
|
|
HEAD = "HEAD" |
|
|
|
|
|
|
|
PATCH = "PATCH" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// GetSupported is the interface that provides the Get
|
|
|
|
// GetSupported is the interface that provides the Get
|
|
|
|
// method a resource must support to receive HTTP GETs.
|
|
|
|
// method a resource must support to receive HTTP GETs.
|
|
|
|
type GetSupported interface { |
|
|
|
type GetSupported interface { |
|
|
|
Get(url.Values, http.Header) (int, interface{}, http.Header) |
|
|
|
Get(*http.Request) (int, interface{}, http.Header) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// PostSupported is the interface that provides the Post
|
|
|
|
// PostSupported is the interface that provides the Post
|
|
|
|
// method a resource must support to receive HTTP POSTs.
|
|
|
|
// method a resource must support to receive HTTP POSTs.
|
|
|
|
type PostSupported interface { |
|
|
|
type PostSupported interface { |
|
|
|
Post(url.Values, http.Header) (int, interface{}, http.Header) |
|
|
|
Post(*http.Request) (int, interface{}, http.Header) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// PutSupported is the interface that provides the Put
|
|
|
|
// PutSupported is the interface that provides the Put
|
|
|
|
// method a resource must support to receive HTTP PUTs.
|
|
|
|
// method a resource must support to receive HTTP PUTs.
|
|
|
|
type PutSupported interface { |
|
|
|
type PutSupported interface { |
|
|
|
Put(url.Values, http.Header) (int, interface{}, http.Header) |
|
|
|
Put(*http.Request) (int, interface{}, http.Header) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// DeleteSupported is the interface that provides the Delete
|
|
|
|
// DeleteSupported is the interface that provides the Delete
|
|
|
|
// method a resource must support to receive HTTP DELETEs.
|
|
|
|
// method a resource must support to receive HTTP DELETEs.
|
|
|
|
type DeleteSupported interface { |
|
|
|
type DeleteSupported interface { |
|
|
|
Delete(url.Values, http.Header) (int, interface{}, http.Header) |
|
|
|
Delete(*http.Request) (int, interface{}, http.Header) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// HeadSupported is the interface that provides the Head
|
|
|
|
|
|
|
|
// method a resource must support to receive HTTP HEADs.
|
|
|
|
|
|
|
|
type HeadSupported interface { |
|
|
|
|
|
|
|
Head(*http.Request) (int, interface{}, http.Header) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// PatchSupported is the interface that provides the Patch
|
|
|
|
|
|
|
|
// method a resource must support to receive HTTP PATCHs.
|
|
|
|
|
|
|
|
type PatchSupported interface { |
|
|
|
|
|
|
|
Patch(*http.Request) (int, interface{}, http.Header) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Interface for arbitrary muxer support (like http.ServeMux).
|
|
|
|
// Interface for arbitrary muxer support (like http.ServeMux).
|
|
|
@ -99,7 +112,7 @@ func (api *API) requestHandler(resource interface{}) http.HandlerFunc { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var handler func(url.Values, http.Header) (int, interface{}, http.Header) |
|
|
|
var handler func(*http.Request) (int, interface{}, http.Header) |
|
|
|
|
|
|
|
|
|
|
|
switch request.Method { |
|
|
|
switch request.Method { |
|
|
|
case GET: |
|
|
|
case GET: |
|
|
@ -118,6 +131,14 @@ func (api *API) requestHandler(resource interface{}) http.HandlerFunc { |
|
|
|
if resource, ok := resource.(DeleteSupported); ok { |
|
|
|
if resource, ok := resource.(DeleteSupported); ok { |
|
|
|
handler = resource.Delete |
|
|
|
handler = resource.Delete |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
case HEAD: |
|
|
|
|
|
|
|
if resource, ok := resource.(HeadSupported); ok { |
|
|
|
|
|
|
|
handler = resource.Head |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
case PATCH: |
|
|
|
|
|
|
|
if resource, ok := resource.(PatchSupported); ok { |
|
|
|
|
|
|
|
handler = resource.Patch |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if handler == nil { |
|
|
|
if handler == nil { |
|
|
@ -125,7 +146,7 @@ func (api *API) requestHandler(resource interface{}) http.HandlerFunc { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
code, data, header := handler(request.Form, request.Header) |
|
|
|
code, data, header := handler(request) |
|
|
|
|
|
|
|
|
|
|
|
content, err := json.MarshalIndent(data, "", " ") |
|
|
|
content, err := json.MarshalIndent(data, "", " ") |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|