diff options
author | Doug Black <dblack@twilio.com> | 2014-01-27 11:20:22 -0500 |
---|---|---|
committer | Doug Black <dblack@twilio.com> | 2014-01-27 11:20:22 -0500 |
commit | 6ba542f49d739ef4c94bc28f552d5519d9d75151 (patch) | |
tree | 3def11e9c9ee1a5e6297daf8151943bb9d39f2dd /core.go | |
parent | export 'method supported' interfaces (diff) | |
download | sleepy-6ba542f49d739ef4c94bc28f552d5519d9d75151.tar.gz sleepy-6ba542f49d739ef4c94bc28f552d5519d9d75151.tar.bz2 sleepy-6ba542f49d739ef4c94bc28f552d5519d9d75151.zip |
add docs
Diffstat (limited to 'core.go')
-rw-r--r-- | core.go | 20 |
1 files changed, 20 insertions, 0 deletions
@@ -14,30 +14,46 @@ const ( DELETE = "DELETE" ) +// GetSupported is the interface that provides the Get +// method a resource must support to receive HTTP GETs. type GetSupported interface { Get(values url.Values) (int, interface{}) } +// PostSupported is the interface that provides the Get +// method a resource must support to receive HTTP GETs. type PostSupported interface { Post(values url.Values) (int, interface{}) } +// PutSupported is the interface that provides the Get +// method a resource must support to receive HTTP GETs. type PutSupported interface { Put(values url.Values) (int, interface{}) } +// DeleteSupported is the interface that provides the Get +// method a resource must support to receive HTTP GETs. type DeleteSupported interface { Delete(values url.Values) (int, interface{}) } +// An API manages a group of resources by routing to requests +// to the correct method on a matching resource. +// +// You can instantiate multiple APIs on separate ports. Each API +// will manage its own set of resources. type API struct { mux *http.ServeMux } +// NewAPI allocates and returns a new API. func NewAPI() *API { return &API{} } +// Abort responds to a given request with the status text associated +// with the passed in HTTP status code. func (api *API) Abort(rw http.ResponseWriter, statusCode int) { rw.WriteHeader(statusCode) rw.Write([]byte(http.StatusText(statusCode))) @@ -100,6 +116,9 @@ func (api *API) requestHandler(resource interface{}) http.HandlerFunc { } } +// AddResource adds a new resource to an API. The API will route +// request matching the path to the correct HTTP method on the +// resource. func (api *API) AddResource(resource interface{}, path string) { if api.mux == nil { api.mux = http.NewServeMux() @@ -107,6 +126,7 @@ func (api *API) AddResource(resource interface{}, path string) { api.mux.HandleFunc(path, api.requestHandler(resource)) } +// Start causes the API to begin serving requests on the given port. func (api *API) Start(port int) error { if api.mux == nil { return &ErrorString{"You must add at least one resource to this API."} |