From 1f1580b678ff0b859e0b08e11e10e29f49cd4680 Mon Sep 17 00:00:00 2001 From: Doug Black Date: Mon, 27 Jan 2014 11:14:08 -0500 Subject: export 'method supported' interfaces --- core.go | 52 +++++++++++++++++++++++++++++++++++----------------- error.go | 4 ++-- tests/test.go | 2 +- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/core.go b/core.go index 73d3e0a..b159fdd 100644 --- a/core.go +++ b/core.go @@ -14,19 +14,19 @@ const ( DELETE = "DELETE" ) -type geter interface { +type GetSupported interface { Get(values url.Values) (int, interface{}) } -type poster interface { +type PostSupported interface { Post(values url.Values) (int, interface{}) } -type puter interface { +type PutSupported interface { Put(values url.Values) (int, interface{}) } -type deleter interface { +type DeleteSupported interface { Delete(values url.Values) (int, interface{}) } @@ -34,6 +34,10 @@ type API struct { mux *http.ServeMux } +func NewAPI() *API { + return &API{} +} + func (api *API) Abort(rw http.ResponseWriter, statusCode int) { rw.WriteHeader(statusCode) rw.Write([]byte(http.StatusText(statusCode))) @@ -41,33 +45,47 @@ func (api *API) Abort(rw http.ResponseWriter, statusCode int) { func (api *API) requestHandler(resource interface{}) http.HandlerFunc { return func(rw http.ResponseWriter, request *http.Request) { + + var err error + var data interface{} = "" + var code int = 405 + method := request.Method if request.ParseForm() != nil { api.Abort(rw, 400) - return + return } values := request.Form - var data interface{} = "" - var code int = 405 - switch method { case GET: - if r, ok := resource.(geter); ok { + if r, ok := resource.(GetSupported); ok { code, data = r.Get(values) - } + } else { + api.Abort(rw, 405) + return + } case POST: - if r, ok := resource.(poster); ok { + if r, ok := resource.(PostSupported); ok { code, data = r.Post(values) - } + } else { + api.Abort(rw, 405) + return + } case PUT: - if r, ok := resource.(puter); ok { + if r, ok := resource.(PutSupported); ok { code, data = r.Put(values) - } + } else { + api.Abort(rw, 405) + return + } case DELETE: - if r, ok := resource.(deleter); ok { + if r, ok := resource.(DeleteSupported); ok { code, data = r.Delete(values) - } + } else { + api.Abort(rw, 405) + return + } default: api.Abort(rw, 405) return @@ -91,7 +109,7 @@ func (api *API) AddResource(resource interface{}, path string) { func (api *API) Start(port int) error { if api.mux == nil { - return &errorString{"You must add at last one resource to this API."} + return &ErrorString{"You must add at least one resource to this API."} } portString := fmt.Sprintf(":%d", port) http.ListenAndServe(portString, api.mux) diff --git a/error.go b/error.go index c52a483..dc129cb 100644 --- a/error.go +++ b/error.go @@ -1,9 +1,9 @@ package sleepy -type errorString struct { +type ErrorString struct { s string } -func (e *errorString) Error() string { +func (e *ErrorString) Error() string { return e.s } diff --git a/tests/test.go b/tests/test.go index b36cdb6..fededa1 100644 --- a/tests/test.go +++ b/tests/test.go @@ -18,7 +18,7 @@ func main() { item := new(Item) - var api = new(sleepy.API) + var api = sleepy.NewAPI() api.AddResource(item, "/items") api.Start(3000) -- cgit