diff --git a/rest/collection.go b/rest/collection.go deleted file mode 100755 index 6e4f40a..0000000 --- a/rest/collection.go +++ /dev/null @@ -1,95 +0,0 @@ -package rest - -import ( - "net/http" - "net/url" - "path/filepath" - "sync" - - "git.sdf.org/jchenry/x/encoding" - "git.sdf.org/jchenry/x/log" -) - -// Example: Resource(p, c, JSONEncoder, json.Decode(func()interface{}{return &foo{}}), log.None{}) -func Resource(p *sync.Pool, g Gateway, e EntityEncoder, d encoding.Decoder, l log.Interface) http.HandlerFunc { - return restVerbHandler( - GetResource(g, e, l), - PostResource(g, d, p, l), - PutResource(g, e, d, p, l), - DeleteResource(g, l), - ) -} - -func GetResource(store Readable, encode EntityEncoder, log log.Interface) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { // GET - if id := filepath.Base(r.URL.Path); id != "" { - if e, err := store.Read(id); err == nil { // handle individual entity - encode(w, e) - } else { - w.WriteHeader(http.StatusInternalServerError) - log.Printf("Error: %s", err) - } - } else { - if params, err := url.ParseQuery(r.URL.RawQuery); err == nil { - if e, err := store.All(params); err == nil { // handle all entities - encode(w, e) - } else { - w.WriteHeader(http.StatusInternalServerError) - log.Printf("Error: %s", err) - } - } else { - w.WriteHeader(http.StatusBadRequest) - } - } - } -} - -func PostResource(store Creatable, decode encoding.Decoder, pool *sync.Pool, log log.Interface) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { // POST TODO - e := pool.Get() - defer pool.Put(e) - if err := decode(r.Body, e); err == nil { - if err = store.Create(e); err == nil { - w.WriteHeader(http.StatusCreated) - } else { - w.WriteHeader(http.StatusInternalServerError) - log.Printf("Error: %s", err) - } - } else { - w.WriteHeader(http.StatusBadRequest) - } - } -} - -func PutResource(store Updatable, encode EntityEncoder, decode encoding.Decoder, pool *sync.Pool, log log.Interface) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { // PUT TODO - e := pool.Get() - defer pool.Put(e) - if err := decode(r.Body, e); err == nil { - if err = store.Update(e); err == nil { - w.WriteHeader(http.StatusAccepted) - encode(w, e) - } else { - w.WriteHeader(http.StatusInternalServerError) - log.Printf("Error: %s", err) - } - } else { - w.WriteHeader(http.StatusBadRequest) - } - } -} - -func DeleteResource(store Deletable, log log.Interface) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { // DELETE TODO - if id := filepath.Base(r.URL.Path); id != "" { - if err := store.Delete(id); err == nil { - w.WriteHeader(http.StatusNoContent) - } else { - w.WriteHeader(http.StatusInternalServerError) - log.Printf("Error: %s", err) - } - } else { - w.WriteHeader(http.StatusBadRequest) - } - } -} diff --git a/rest/doc.go b/rest/doc.go deleted file mode 100644 index 8cef5c6..0000000 --- a/rest/doc.go +++ /dev/null @@ -1,5 +0,0 @@ -package rest - -// Deprecated: I wrote this code when i was foolish enough to think you should use such things. -// I know better now. http.Handlers are properly structured helper functions are enough. -// I will remove this at some point //TODO diff --git a/rest/entity_handler.go b/rest/entity_handler.go deleted file mode 100644 index cf53f31..0000000 --- a/rest/entity_handler.go +++ /dev/null @@ -1,23 +0,0 @@ -package rest - -import ( - gohttp "net/http" - - "git.sdf.org/jchenry/x/net/http" -) - -// EntityHandler returns a handler that provides restful verbs, following a CRUD model -func restVerbHandler( - get gohttp.Handler, - post gohttp.Handler, - put gohttp.Handler, - delete gohttp.Handler, -) gohttp.HandlerFunc { - h, _ := http.MutliHandler(map[string]gohttp.Handler{ - gohttp.MethodGet: get, - gohttp.MethodPost: post, - gohttp.MethodPut: put, - gohttp.MethodDelete: delete, - }) - return h -} diff --git a/rest/gateway.go b/rest/gateway.go deleted file mode 100644 index b53207c..0000000 --- a/rest/gateway.go +++ /dev/null @@ -1,25 +0,0 @@ -package rest - -type Creatable interface { - Create(e interface{}) error -} - -type Updatable interface { - Update(e interface{}) error -} - -type Deletable interface { - Delete(id string) error -} - -type Readable interface { - All(filters map[string][]string) (interface{}, error) - Read(id string) (interface{}, error) -} - -type Gateway interface { - Creatable - Updatable - Deletable - Readable -} diff --git a/rest/response_encoder.go b/rest/response_encoder.go deleted file mode 100644 index 3b95dbc..0000000 --- a/rest/response_encoder.go +++ /dev/null @@ -1,32 +0,0 @@ -package rest - -import ( - "net/http" - - "git.sdf.org/jchenry/x/encoding" - "git.sdf.org/jchenry/x/encoding/json" - "git.sdf.org/jchenry/x/encoding/xml" -) - -type EntityEncoder func(w http.ResponseWriter, e interface{}) - -func JSONEncoder(w http.ResponseWriter, e interface{}) error { - return EntityResponseEncoder(w, "application/json", json.Encoder, e) -} - -func XMLEncoder(w http.ResponseWriter, e interface{}) error { - return EntityResponseEncoder(w, "application/xml", xml.Encoder, e) -} - -func EntityResponseEncoder(w http.ResponseWriter, contentType string, encoder encoding.Encoder, e interface{}) error { - w.Header().Set("content-type", contentType) - return encoder(w, e) -} - -func ErrorResponseEncoder(w http.ResponseWriter, contentType string, encoder encoding.Encoder, status int, err error) error { - w.WriteHeader(status) - return EntityResponseEncoder(w, contentType, encoder, map[string]interface{}{ - "status": status, - "message": err.Error, - }) -}