From 0a2d08f88e0a25d8fe388647abdfad029ca6a0b7 Mon Sep 17 00:00:00 2001 From: Doug Black Date: Mon, 27 Jan 2014 10:41:28 -0500 Subject: use ServeMux, capitalize API, clean tests --- core.go | 24 +++++++++++++++++------- error.go | 9 +++++++++ tests/items.go | 27 --------------------------- tests/one.go | 14 -------------- tests/test.go | 17 +++++++++++------ 5 files changed, 37 insertions(+), 54 deletions(-) create mode 100644 error.go delete mode 100644 tests/items.go delete mode 100644 tests/one.go diff --git a/core.go b/core.go index 7f7d0b1..0c4cfbd 100644 --- a/core.go +++ b/core.go @@ -21,14 +21,17 @@ type Resource 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 Resource) http.HandlerFunc { + +func (api *API) requestHandler(resource Resource) http.HandlerFunc { return func(rw http.ResponseWriter, request *http.Request) { var data interface{} @@ -64,12 +67,19 @@ func (api *Api) requestHandler(resource Resource) http.HandlerFunc { } } -func (api *Api) AddResource(resource Resource, path string) { - http.HandleFunc(path, api.requestHandler(resource)) +func (api *API) AddResource(resource Resource, 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) fmt.Println("Hi.") + 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 978b052..0000000 --- a/tests/items.go +++ /dev/null @@ -1,27 +0,0 @@ -package main - -import ( - "net/url" - "sleepy" -) - -type Item struct { - sleepy.BaseResource -} - -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 d3f414c..adff27a 100644 --- a/tests/test.go +++ b/tests/test.go @@ -5,18 +5,23 @@ import ( "sleepy" ) -type Bar struct { +type Item struct { sleepy.BaseResource } -func (b Bar) Get(values url.Values) (int, interface{}) { - return 200, map[string]string{"hello": "goodbye"} +func (item Item) Get(values url.Values) (int, interface{}) { + items := []string{"item1", "item2"} + data := map[string][]string{"items": items} + + 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) + } -- cgit