From 4fe5a5345033aa03a3dcd035552d9951432ea21e Mon Sep 17 00:00:00 2001 From: Colin Henry Date: Sat, 22 Aug 2020 15:54:49 -0700 Subject: [PATCH] added support for new logger in rest.Collection --- rest/collection.go | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/rest/collection.go b/rest/collection.go index 32d9819..d313365 100755 --- a/rest/collection.go +++ b/rest/collection.go @@ -7,6 +7,7 @@ import ( "sync" "github.com/jchenry/x/encoding" + "github.com/jchenry/x/log" ) type CollectionStore interface { @@ -17,20 +18,17 @@ type CollectionStore interface { New(e interface{}) error } -// Example: Collection(p, c, JSONEncoder, json.Decode(func()interface{}{return &foo{}})) - -// type Decoder func(io.Reader) (interface{}, error) - -func Collection(pool *sync.Pool, store CollectionStore, encode EntityEncoder, decode encoding.Decoder) http.HandlerFunc { +// Example: Collection(p, c, JSONEncoder, json.Decode(func()interface{}{return &foo{}}), log.None{}) +func Collection(pool *sync.Pool, store CollectionStore, encode EntityEncoder, decode encoding.Decoder, log log.Logger) http.HandlerFunc { return EntityHandler( - collectionGet(store, encode), - collectionPost(store, encode, decode, pool), - collectionPut(store, encode, decode, pool), - collectionDelete(store, encode), + collectionGet(store, encode, log), + collectionPost(store, encode, decode, pool, log), + collectionPut(store, encode, decode, pool, log), + collectionDelete(store, encode, log), ) } -func collectionGet(store CollectionStore, encode EntityEncoder) http.HandlerFunc { +func collectionGet(store CollectionStore, encode EntityEncoder, log log.Logger) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { // GET if id := filepath.Base(r.URL.Path); id != "" { if e, err := store.Get(id); err == nil { // handle individual entity @@ -38,6 +36,7 @@ func collectionGet(store CollectionStore, encode EntityEncoder) http.HandlerFunc } else { w.WriteHeader(http.StatusInternalServerError) encode(w, err) + log.Printf("Error: %s", err) } } else { if params, err := url.ParseQuery(r.URL.RawQuery); err == nil { @@ -46,6 +45,7 @@ func collectionGet(store CollectionStore, encode EntityEncoder) http.HandlerFunc } else { // TODO: we really should write a header here, but need to figure out what it should be w.WriteHeader(http.StatusInternalServerError) + log.Printf("Error: %s", err) } } else { // encode(w, err) @@ -56,7 +56,7 @@ func collectionGet(store CollectionStore, encode EntityEncoder) http.HandlerFunc } } -func collectionPost(store CollectionStore, encode EntityEncoder, decode encoding.Decoder, pool *sync.Pool) http.HandlerFunc { +func collectionPost(store CollectionStore, encode EntityEncoder, decode encoding.Decoder, pool *sync.Pool, log log.Logger) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { // POST TODO e := pool.New() defer pool.Put(e) @@ -65,6 +65,7 @@ func collectionPost(store CollectionStore, encode EntityEncoder, decode encoding w.WriteHeader(http.StatusCreated) } else { w.WriteHeader(http.StatusInternalServerError) + log.Printf("Error: %s", err) } } else { w.WriteHeader(http.StatusBadRequest) @@ -72,7 +73,7 @@ func collectionPost(store CollectionStore, encode EntityEncoder, decode encoding } } -func collectionPut(store CollectionStore, encode EntityEncoder, decode encoding.Decoder, pool *sync.Pool) http.HandlerFunc { +func collectionPut(store CollectionStore, encode EntityEncoder, decode encoding.Decoder, pool *sync.Pool, log log.Logger) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { // PUT TODO e := pool.New() defer pool.Put(e) @@ -82,7 +83,7 @@ func collectionPut(store CollectionStore, encode EntityEncoder, decode encoding. encode(w, e) } else { w.WriteHeader(http.StatusInternalServerError) - encode(w, err) + log.Printf("Error: %s", err) } } else { w.WriteHeader(http.StatusBadRequest) @@ -92,13 +93,14 @@ func collectionPut(store CollectionStore, encode EntityEncoder, decode encoding. } } -func collectionDelete(store CollectionStore, encode EntityEncoder) http.HandlerFunc { +func collectionDelete(store CollectionStore, encode EntityEncoder, log log.Logger) 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)