diff options
author | Doug Black <dougblack@gatech.edu> | 2014-01-23 23:27:43 -0800 |
---|---|---|
committer | Doug Black <dougblack@gatech.edu> | 2014-01-23 23:27:43 -0800 |
commit | e2c9a522cf0dd83e1bec9982e7aa5197f810fb8c (patch) | |
tree | 14e40487b54dc90b61c77d8e3e900e73ba37f101 /core.go | |
parent | fix typo (diff) | |
download | sleepy-e2c9a522cf0dd83e1bec9982e7aa5197f810fb8c.tar.gz sleepy-e2c9a522cf0dd83e1bec9982e7aa5197f810fb8c.tar.bz2 sleepy-e2c9a522cf0dd83e1bec9982e7aa5197f810fb8c.zip |
add mechanism for specifying unimplemented methods and dynamic dispatch to a resource based on HTTP method
Diffstat (limited to 'core.go')
-rw-r--r-- | core.go | 40 |
1 files changed, 38 insertions, 2 deletions
@@ -5,7 +5,27 @@ import ( ) type Resource interface { - Get() string + Get(map[string][]string) string + Post(map[string][]string) string + Put(map[string][]string) string + Delete(map[string][]string) string +} + +type GetNotSupported struct {} +func (GetNotSupported) Get(map[string][]string) string { + return "Nope." +} +type PostNotSupported struct {} +func (PostNotSupported) Post(map[string][]string) string { + return "Nope." +} +type PutNotSupported struct {} +func (PutNotSupported) Put(map[string][]string) string { + return "Nope." +} +type DeleteNotSupported struct {} +func (DeleteNotSupported) Delete(map[string][]string) string { + return "Nope." } type Route struct { @@ -30,9 +50,25 @@ func (api *Api) matchResource(path string) Resource { return nil } +func (api *Api) dispatchRequest(request *http.Request, resource Resource) string { + method := request.Method + + switch method { + case "GET": + return resource.Get(nil) + case "POST": + return resource.Post(nil) + case "PUT": + return resource.Put(nil) + case "DELETE": + return resource.Delete(nil) + } + return "Not implemented!" +} + func (api *Api) HandleRequest(request *http.Request) string { resource := api.matchResource(request.URL.Path) - return resource.Get() + return api.dispatchRequest(request, resource) } func (api *Api) AddResource(resource Resource, path string) { |