aboutsummaryrefslogtreecommitdiff
path: root/core.go
diff options
context:
space:
mode:
authorDoug Black <dblack@twilio.com>2014-01-27 10:41:28 -0500
committerDoug Black <dblack@twilio.com>2014-01-27 10:41:28 -0500
commit0a2d08f88e0a25d8fe388647abdfad029ca6a0b7 (patch)
tree31ed7a9147b4ac5b4354b00edcaa1f0db4c386b4 /core.go
parentadd license (diff)
downloadsleepy-0a2d08f88e0a25d8fe388647abdfad029ca6a0b7.tar.gz
sleepy-0a2d08f88e0a25d8fe388647abdfad029ca6a0b7.tar.bz2
sleepy-0a2d08f88e0a25d8fe388647abdfad029ca6a0b7.zip
use ServeMux, capitalize API, clean tests
Diffstat (limited to 'core.go')
-rw-r--r--core.go24
1 files changed, 17 insertions, 7 deletions
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
}
bgstack15