aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Black <dougblack@gatech.edu>2014-01-23 23:27:43 -0800
committerDoug Black <dougblack@gatech.edu>2014-01-23 23:27:43 -0800
commite2c9a522cf0dd83e1bec9982e7aa5197f810fb8c (patch)
tree14e40487b54dc90b61c77d8e3e900e73ba37f101
parentfix typo (diff)
downloadsleepy-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
-rw-r--r--core.go40
1 files changed, 38 insertions, 2 deletions
diff --git a/core.go b/core.go
index c1c0a43..c6f56ad 100644
--- a/core.go
+++ b/core.go
@@ -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) {
bgstack15