From 69de76cb32cfd638672d4d5846d0659bf102316f Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Tue, 17 May 2022 15:54:20 -0600 Subject: Add asset file upload form, plus related necessary refactors --- srv/src/api/apiutil/apiutil.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'srv/src/api/apiutil') diff --git a/srv/src/api/apiutil/apiutil.go b/srv/src/api/apiutil/apiutil.go index c9f8795..f7830ae 100644 --- a/srv/src/api/apiutil/apiutil.go +++ b/srv/src/api/apiutil/apiutil.go @@ -11,6 +11,7 @@ import ( "fmt" "net/http" "strconv" + "strings" "github.com/mediocregopher/mediocre-go-lib/v2/mlog" ) @@ -110,3 +111,23 @@ func RandStr(numBytes int) string { } return hex.EncodeToString(b) } + +// MethodMux will take the request method (GET, POST, etc...) and handle the +// request using the corresponding Handler in the given map. +// +// If no Handler is defined for a method then a 405 Method Not Allowed error is +// returned. +func MethodMux(handlers map[string]http.Handler) http.Handler { + + return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + + handler, ok := handlers[strings.ToUpper(r.Method)] + + if !ok { + http.Error(rw, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + handler.ServeHTTP(rw, r) + }) +} -- cgit v1.2.3