diff options
author | Doug Black <dblack@twilio.com> | 2014-01-27 12:09:18 -0500 |
---|---|---|
committer | Doug Black <dblack@twilio.com> | 2014-01-27 12:09:18 -0500 |
commit | c60cc1e97e7c8af2cbf5c122717035c7f48aae24 (patch) | |
tree | cf704d09bf148822dd2b62fb90690eccb2be6bc0 | |
parent | update README (diff) | |
download | sleepy-c60cc1e97e7c8af2cbf5c122717035c7f48aae24.tar.gz sleepy-c60cc1e97e7c8af2cbf5c122717035c7f48aae24.tar.bz2 sleepy-c60cc1e97e7c8af2cbf5c122717035c7f48aae24.zip |
formatting and use 'errors'
-rw-r--r-- | core.go | 47 | ||||
-rw-r--r-- | error.go | 12 | ||||
-rw-r--r-- | tests/test.go | 4 |
3 files changed, 26 insertions, 37 deletions
@@ -2,6 +2,7 @@ package sleepy import ( "encoding/json" + "errors" "fmt" "net/http" "net/url" @@ -44,12 +45,12 @@ type DeleteSupported interface { // You can instantiate multiple APIs on separate ports. Each API // will manage its own set of resources. type API struct { - mux *http.ServeMux + mux *http.ServeMux } // NewAPI allocates and returns a new API. func NewAPI() *API { - return &API{} + return &API{} } // Abort responds to a given request with the status text associated @@ -62,14 +63,14 @@ 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 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 @@ -78,30 +79,30 @@ func (api *API) requestHandler(resource interface{}) http.HandlerFunc { if r, ok := resource.(GetSupported); ok { code, data = r.Get(values) } else { - api.Abort(rw, 405) - return - } + api.Abort(rw, 405) + return + } case POST: if r, ok := resource.(PostSupported); ok { code, data = r.Post(values) } else { - api.Abort(rw, 405) - return - } + api.Abort(rw, 405) + return + } case PUT: if r, ok := resource.(PutSupported); ok { code, data = r.Put(values) } else { - api.Abort(rw, 405) - return - } + api.Abort(rw, 405) + return + } case DELETE: if r, ok := resource.(DeleteSupported); ok { code, data = r.Delete(values) } else { - api.Abort(rw, 405) - return - } + api.Abort(rw, 405) + return + } default: api.Abort(rw, 405) return @@ -120,18 +121,18 @@ func (api *API) requestHandler(resource interface{}) http.HandlerFunc { // request matching the path to the correct HTTP method on the // resource. func (api *API) AddResource(resource interface{}, path string) { - if api.mux == nil { - api.mux = http.NewServeMux() - } + if api.mux == nil { + api.mux = http.NewServeMux() + } api.mux.HandleFunc(path, api.requestHandler(resource)) } // Start causes the API to begin serving requests on the given port. func (api *API) Start(port int) error { - if api.mux == nil { - return &ErrorString{"You must add at least one resource to this API."} - } + if api.mux == nil { + return errors.New("You must add at least one resource to this API.") + } portString := fmt.Sprintf(":%d", port) http.ListenAndServe(portString, api.mux) - return nil + return nil } diff --git a/error.go b/error.go deleted file mode 100644 index 0ad5a42..0000000 --- a/error.go +++ /dev/null @@ -1,12 +0,0 @@ -package sleepy - -// ErrorString is an Error that returns a string -// representation. -type ErrorString struct { - s string -} - -// Returns the string associated with this ErrorString. -func (e *ErrorString) Error() string { - return e.s -} diff --git a/tests/test.go b/tests/test.go index fededa1..6e0d936 100644 --- a/tests/test.go +++ b/tests/test.go @@ -1,11 +1,11 @@ package main import ( - "net/url" "github.com/dougblack/sleepy" + "net/url" ) -type Item struct {} +type Item struct{} func (item Item) Get(values url.Values) (int, interface{}) { items := []string{"item1", "item2"} |