summaryrefslogtreecommitdiff
path: root/srv/src/api/apiutil
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2022-05-17 15:54:20 -0600
committerBrian Picciano <mediocregopher@gmail.com>2022-05-17 15:54:20 -0600
commit69de76cb32cfd638672d4d5846d0659bf102316f (patch)
treefd2a1f9fdd815a70eb514b54dbcacc24b8e402cc /srv/src/api/apiutil
parente406ad6e7c82592d3bbaa1cf93ffc1612e4f196c (diff)
Add asset file upload form, plus related necessary refactors
Diffstat (limited to 'srv/src/api/apiutil')
-rw-r--r--srv/src/api/apiutil/apiutil.go21
1 files changed, 21 insertions, 0 deletions
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)
+ })
+}