diff options
-rw-r--r-- | AUTHORS.md | 8 | ||||
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | core.go | 40 | ||||
-rw-r--r-- | http.go | 23 | ||||
-rw-r--r-- | tests/test.go | 6 |
6 files changed, 46 insertions, 42 deletions
diff --git a/AUTHORS.md b/AUTHORS.md new file mode 100644 index 0000000..f12478e --- /dev/null +++ b/AUTHORS.md @@ -0,0 +1,8 @@ +Authors +======= + +A huge thanks to all of our contributors: + + +- Doug Black +- strandmon diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fa7016d --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +all: + +authors: + echo "Authors\n=======\n\nA huge thanks to all of our contributors:\n\n" > AUTHORS.md + git log --raw | grep "^Author: " | cut -d ' ' -f2- | cut -d '<' -f1 | sed 's/^/- /' | sort | uniq >> AUTHORS.md @@ -9,11 +9,15 @@ package main import ( "net/url" + "github.com/dougblack/sleepy" ) type Item struct { +<<<<<<< HEAD sleepy.BaseResource +======= +>>>>>>> da9d8e922d0198a324af1f1b6df97d3e1d5938aa } func (item Item) Get(values url.Values) (int, interface{}) { @@ -24,13 +28,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{}) } @@ -30,13 +39,8 @@ func (api *API) Abort(rw http.ResponseWriter, statusCode int) { rw.Write([]byte(http.StatusText(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) @@ -44,15 +48,26 @@ 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 @@ -67,7 +82,7 @@ func (api *API) requestHandler(resource Resource) http.HandlerFunc { } } -func (api *API) AddResource(resource Resource, path string) { +func (api *API) AddResource(resource interface{}, path string) { if api.mux == nil { api.mux = http.NewServeMux() } @@ -80,6 +95,5 @@ func (api *API) Start(port int) error { } portString := fmt.Sprintf(":%d", port) http.ListenAndServe(portString, api.mux) - fmt.Println("Hi.") return nil } diff --git a/http.go b/http.go deleted file mode 100644 index 8efe657..0000000 --- a/http.go +++ /dev/null @@ -1,23 +0,0 @@ -package sleepy - -import ( - "net/url" -) - -type BaseResource struct{} - -func (BaseResource) Get(values url.Values) (int, interface{}) { - return 405, "" -} - -func (BaseResource) Post(values url.Values) (int, interface{}) { - return 405, "" -} - -func (BaseResource) Put(values url.Values) (int, interface{}) { - return 405, "" -} - -func (BaseResource) Delete(values url.Values) (int, interface{}) { - return 405, "" -} diff --git a/tests/test.go b/tests/test.go index adff27a..b36cdb6 100644 --- a/tests/test.go +++ b/tests/test.go @@ -2,12 +2,10 @@ package main import ( "net/url" - "sleepy" + "github.com/dougblack/sleepy" ) -type Item struct { - sleepy.BaseResource -} +type Item struct {} func (item Item) Get(values url.Values) (int, interface{}) { items := []string{"item1", "item2"} |