diff options
-rw-r--r-- | AUTHORS.md | 8 | ||||
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | README.md | 8 | ||||
-rw-r--r-- | core.go | 32 | ||||
-rw-r--r-- | error.go | 9 | ||||
-rw-r--r-- | tests/items.go | 25 | ||||
-rw-r--r-- | tests/one.go | 14 | ||||
-rw-r--r-- | tests/test.go | 19 |
8 files changed, 62 insertions, 58 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 @@ -14,6 +14,10 @@ import ( ) type Item struct { +<<<<<<< HEAD + sleepy.BaseResource +======= +>>>>>>> da9d8e922d0198a324af1f1b6df97d3e1d5938aa } func (item Item) Get(values url.Values) (int, interface{}) { @@ -40,3 +44,7 @@ curl localhost:3000/items ``` Stay tuned. + +## License + +Sleepy is released under the [MIT License](http://opensource.org/licenses/MIT). @@ -30,13 +30,16 @@ type deleter interface { Delete(values url.Values) (int, interface{}) } -type Api struct{} +type API struct { + mux *http.ServeMux +} -func (api *Api) Abort(rw http.ResponseWriter, statusCode int) { +func (api *API) Abort(rw http.ResponseWriter, statusCode int) { rw.WriteHeader(statusCode) + rw.Write([]byte(http.StatusText(statusCode))) } -func (api *Api) requestHandler(resource interface{}) http.HandlerFunc { +func (api *API) requestHandler(resource interface{}) http.HandlerFunc { return func(rw http.ResponseWriter, request *http.Request) { method := request.Method if request.ParseForm() != nil { @@ -70,20 +73,27 @@ func (api *Api) requestHandler(resource interface{}) http.HandlerFunc { return } - responseWriter := json.NewEncoder(rw) - rw.WriteHeader(code) - if responseWriter.Encode(data) != nil { + content, err := json.Marshal(data) + if err != nil { api.Abort(rw, 500) - return } + rw.WriteHeader(code) + rw.Write(content) } } -func (api *Api) AddResource(resource interface{}, path string) { - http.HandleFunc(path, api.requestHandler(resource)) +func (api *API) AddResource(resource interface{}, path string) { + if api.mux == nil { + api.mux = http.NewServeMux() + } + api.mux.HandleFunc(path, api.requestHandler(resource)) } -func (api *Api) Start(port int) { +func (api *API) Start(port int) error { + if api.mux == nil { + return &errorString{"You must add at last one resource to this API."} + } portString := fmt.Sprintf(":%d", port) - http.ListenAndServe(portString, nil) + http.ListenAndServe(portString, api.mux) + return nil } diff --git a/error.go b/error.go new file mode 100644 index 0000000..c52a483 --- /dev/null +++ b/error.go @@ -0,0 +1,9 @@ +package sleepy + +type errorString struct { + s string +} + +func (e *errorString) Error() string { + return e.s +} diff --git a/tests/items.go b/tests/items.go deleted file mode 100644 index 6aae382..0000000 --- a/tests/items.go +++ /dev/null @@ -1,25 +0,0 @@ -package main - -import ( - "net/url" - - "github.com/kid0m4n/sleepy" -) - -type Item struct { -} - -func (item Item) Get(values url.Values) (int, interface{}) { - items := []string{"item1", "item2"} - data := map[string][]string{"items": items} - - return 200, data -} - -func main() { - item := new(Item) - - var api = new(sleepy.Api) - api.AddResource(item, "/items") - api.Start(3000) -} diff --git a/tests/one.go b/tests/one.go deleted file mode 100644 index 15fd27b..0000000 --- a/tests/one.go +++ /dev/null @@ -1,14 +0,0 @@ -package main - -import ( - "net/http" -) - -func response(rw http.ResponseWriter, request *http.Request) { - rw.Write([]byte("Hello world.")) -} - -func main() { - http.HandleFunc("/", response) - http.ListenAndServe(":3000", nil) -} diff --git a/tests/test.go b/tests/test.go index 8ccbfbd..b36cdb6 100644 --- a/tests/test.go +++ b/tests/test.go @@ -2,21 +2,24 @@ package main import ( "net/url" - "github.com/dougblack/sleepy" ) -type Bar struct { -} +type Item struct {} + +func (item Item) Get(values url.Values) (int, interface{}) { + items := []string{"item1", "item2"} + data := map[string][]string{"items": items} -func (b Bar) Get(values url.Values) (int, interface{}) { - return 200, map[string]string{"hello": "goodbye"} + return 200, data } func main() { - bar := new(Bar) - var api = new(sleepy.Api) - api.AddResource(bar, "/bar") + item := new(Item) + + var api = new(sleepy.API) + api.AddResource(item, "/items") api.Start(3000) + } |