From a2c4a784ef7df53b75962144e31e5f1e45d7d1ee Mon Sep 17 00:00:00 2001 From: Colin Henry Date: Fri, 12 Jun 2020 22:22:58 -0700 Subject: [PATCH] moved some http code to pkg --- {internal => pkg}/http/auth.go | 0 pkg/http/multihandler.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) rename {internal => pkg}/http/auth.go (100%) create mode 100644 pkg/http/multihandler.go diff --git a/internal/http/auth.go b/pkg/http/auth.go similarity index 100% rename from internal/http/auth.go rename to pkg/http/auth.go diff --git a/pkg/http/multihandler.go b/pkg/http/multihandler.go new file mode 100644 index 0000000..8fd8c52 --- /dev/null +++ b/pkg/http/multihandler.go @@ -0,0 +1,31 @@ +package http + +import ( + "fmt" + "net/http" +) + +// MutliHandler takes a map of http methods to handlers +// and returns a handler which will run the the mapped hander +// based on a request's method +func MutliHandler(h map[string]http.Handler) (http.HandlerFunc, error) { + m := map[string]bool{ + http.MethodHead: true, + http.MethodPost: true, + http.MethodPut: true, + http.MethodPatch: true, + http.MethodDelete: true, + http.MethodConnect: true, + http.MethodOptions: true, + http.MethodTrace: true, + } + + for verb := range h { + if _, ok := m[verb]; !ok { + return nil, fmt.Errorf("invalid HTTP method: %s", verb) + } + } + return func(w http.ResponseWriter, r *http.Request) { + h[r.Method].ServeHTTP(w, r) + }, nil +}