removing deprecated rest package
This commit is contained in:
parent
a1e52a7399
commit
612a5c8387
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
@ -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,
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user