aboutsummaryrefslogtreecommitdiff
path: root/core.go
diff options
context:
space:
mode:
authorDoug Black <dblack@twilio.com>2014-01-27 12:09:18 -0500
committerDoug Black <dblack@twilio.com>2014-01-27 12:09:18 -0500
commitc60cc1e97e7c8af2cbf5c122717035c7f48aae24 (patch)
treecf704d09bf148822dd2b62fb90690eccb2be6bc0 /core.go
parentupdate README (diff)
downloadsleepy-c60cc1e97e7c8af2cbf5c122717035c7f48aae24.tar.gz
sleepy-c60cc1e97e7c8af2cbf5c122717035c7f48aae24.tar.bz2
sleepy-c60cc1e97e7c8af2cbf5c122717035c7f48aae24.zip
formatting and use 'errors'
Diffstat (limited to 'core.go')
-rw-r--r--core.go47
1 files changed, 24 insertions, 23 deletions
diff --git a/core.go b/core.go
index ab87024..abf2d15 100644
--- a/core.go
+++ b/core.go
@@ -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
}
bgstack15