aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Black <dblack@twilio.com>2014-01-27 11:14:08 -0500
committerDoug Black <dblack@twilio.com>2014-01-27 11:14:08 -0500
commit1f1580b678ff0b859e0b08e11e10e29f49cd4680 (patch)
tree637540c47991c081d4534559def2a4f1f0ea18fa
parentupdate README (diff)
downloadsleepy-1f1580b678ff0b859e0b08e11e10e29f49cd4680.tar.gz
sleepy-1f1580b678ff0b859e0b08e11e10e29f49cd4680.tar.bz2
sleepy-1f1580b678ff0b859e0b08e11e10e29f49cd4680.zip
export 'method supported' interfaces
-rw-r--r--core.go52
-rw-r--r--error.go4
-rw-r--r--tests/test.go2
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)
bgstack15