diff options
author | Karan Misra <kidoman@gmail.com> | 2014-01-27 10:54:05 +0530 |
---|---|---|
committer | Karan Misra <kidoman@gmail.com> | 2014-01-27 10:54:05 +0530 |
commit | da9d8e922d0198a324af1f1b6df97d3e1d5938aa (patch) | |
tree | 37c8a313dd61e608f453a73073e434774d47fe73 | |
parent | fix signature (diff) | |
download | sleepy-da9d8e922d0198a324af1f1b6df97d3e1d5938aa.tar.gz sleepy-da9d8e922d0198a324af1f1b6df97d3e1d5938aa.tar.bz2 sleepy-da9d8e922d0198a324af1f1b6df97d3e1d5938aa.zip |
remove the need to provide “missing/unsupported” implementations in the
API consumers. also:
- more idiomatic
- loosely coupled with the API contract
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | core.go | 47 | ||||
-rw-r--r-- | http.go | 28 | ||||
-rw-r--r-- | tests/items.go | 8 | ||||
-rw-r--r-- | tests/test.go | 6 |
5 files changed, 36 insertions, 59 deletions
@@ -9,13 +9,11 @@ package main import ( "net/url" + "github.com/dougblack/sleepy" ) type Item struct { - sleepy.PostNotSupported - sleepy.PutNotSupported - sleepy.DeleteNotSupported } func (item Item) Get(values url.Values) (int, interface{}) { @@ -26,13 +24,11 @@ func (item Item) Get(values url.Values) (int, interface{}) { } func main() { - item := new(Item) var api = new(sleepy.Api) api.AddResource(item, "/items") api.Start(3000) - } ``` @@ -14,10 +14,19 @@ const ( DELETE = "DELETE" ) -type Resource interface { +type geter interface { Get(values url.Values) (int, interface{}) +} + +type poster interface { Post(values url.Values) (int, interface{}) +} + +type puter interface { Put(values url.Values) (int, interface{}) +} + +type deleter interface { Delete(values url.Values) (int, interface{}) } @@ -27,12 +36,8 @@ func (api *Api) Abort(rw http.ResponseWriter, statusCode int) { rw.WriteHeader(statusCode) } -func (api *Api) requestHandler(resource Resource) http.HandlerFunc { +func (api *Api) requestHandler(resource interface{}) http.HandlerFunc { return func(rw http.ResponseWriter, request *http.Request) { - - var data interface{} - var code int - method := request.Method if request.ParseForm() != nil { api.Abort(rw, 400) @@ -40,35 +45,45 @@ func (api *Api) requestHandler(resource Resource) http.HandlerFunc { } values := request.Form + var data interface{} = "" + var code int = 405 + switch method { case GET: - code, data = resource.Get(values) + if r, ok := resource.(geter); ok { + code, data = r.Get(values) + } case POST: - code, data = resource.Post(values) + if r, ok := resource.(poster); ok { + code, data = r.Post(values) + } case PUT: - code, data = resource.Put(values) + if r, ok := resource.(puter); ok { + code, data = r.Put(values) + } case DELETE: - code, data = resource.Delete(values) + if r, ok := resource.(deleter); ok { + code, data = r.Delete(values) + } default: api.Abort(rw, 405) return } - responseWriter := json.NewEncoder(rw) + responseWriter := json.NewEncoder(rw) rw.WriteHeader(code) if responseWriter.Encode(data) != nil { - api.Abort(rw, 500) - return - } + api.Abort(rw, 500) + return + } } } -func (api *Api) AddResource(resource Resource, path string) { +func (api *Api) AddResource(resource interface{}, path string) { http.HandleFunc(path, api.requestHandler(resource)) } func (api *Api) Start(port int) { portString := fmt.Sprintf(":%d", port) http.ListenAndServe(portString, nil) - fmt.Println("Hi.") } diff --git a/http.go b/http.go deleted file mode 100644 index 7b79aca..0000000 --- a/http.go +++ /dev/null @@ -1,28 +0,0 @@ -package sleepy - -import ( - "net/url" -) - -type ( - GetNotSupported struct{} - PostNotSupported struct{} - PutNotSupported struct{} - DeleteNotSupported struct{} -) - -func (GetNotSupported) Get(values url.Values) (int, interface{}) { - return 405, "" -} - -func (PostNotSupported) Post(values url.Values) (int, interface{}) { - return 405, "" -} - -func (PutNotSupported) Put(values url.Values) (int, interface{}) { - return 405, "" -} - -func (DeleteNotSupported) Delete(values url.Values) (int, interface{}) { - return 405, "" -} diff --git a/tests/items.go b/tests/items.go index d39cae9..6aae382 100644 --- a/tests/items.go +++ b/tests/items.go @@ -2,13 +2,11 @@ package main import ( "net/url" - "sleepy" + + "github.com/kid0m4n/sleepy" ) type Item struct { - sleepy.PostNotSupported - sleepy.PutNotSupported - sleepy.DeleteNotSupported } func (item Item) Get(values url.Values) (int, interface{}) { @@ -19,11 +17,9 @@ func (item Item) Get(values url.Values) (int, interface{}) { } func main() { - item := new(Item) var api = new(sleepy.Api) api.AddResource(item, "/items") api.Start(3000) - } diff --git a/tests/test.go b/tests/test.go index 070d4b6..8ccbfbd 100644 --- a/tests/test.go +++ b/tests/test.go @@ -2,13 +2,11 @@ package main import ( "net/url" - "sleepy" + + "github.com/dougblack/sleepy" ) type Bar struct { - sleepy.PostNotSupported - sleepy.PutNotSupported - sleepy.DeleteNotSupported } func (b Bar) Get(values url.Values) (int, interface{}) { |