func AcceptHandler(next http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { if r.Header.Get("Accept") != "application/vnd.api+json" { errs.WriteError(w, errs.ErrNotAcceptable) return } next.ServeHTTP(w, r) } return http.HandlerFunc(fn) }
func ContentTypeHandler(next http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { if r.Header.Get("Content-Type") != "application/vnd.api+json" { errs.WriteError(w, errs.ErrUnsupportedMediaType) return } next.ServeHTTP(w, r) } return http.HandlerFunc(fn) }
func RecoverHandler(next http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { defer func() { if err := recover(); err != nil { log.Printf("panic: %+v", err) errs.WriteError(w, errs.ErrInternalServer) } }() next.ServeHTTP(w, r) } return http.HandlerFunc(fn) }
func BodyHandler(v interface{}) func(http.Handler) http.Handler { t := reflect.TypeOf(v) m := func(next http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { val := reflect.New(t).Interface() err := json.NewDecoder(r.Body).Decode(val) if err != nil { errs.WriteError(w, errs.ErrBadRequest) return } if next != nil { context.Set(r, "body", val) next.ServeHTTP(w, r) } } return http.HandlerFunc(fn) } return m }