summaryrefslogtreecommitdiff
path: root/srv/src/http/apiutil/apiutil.go
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2022-05-21 09:17:43 -0600
committerBrian Picciano <mediocregopher@gmail.com>2022-05-21 09:17:43 -0600
commit1de0ab3b720cf7b83a7e29de4dbe35c117ccea0e (patch)
tree3d5441d0b793c23c02b228707acca36649691ed3 /srv/src/http/apiutil/apiutil.go
parent034342421bd0b3d40276df79a0f2450d1fbd643f (diff)
Define an actual middleware type, use that to set up API routes
Diffstat (limited to 'srv/src/http/apiutil/apiutil.go')
-rw-r--r--srv/src/http/apiutil/apiutil.go14
1 files changed, 10 insertions, 4 deletions
diff --git a/srv/src/http/apiutil/apiutil.go b/srv/src/http/apiutil/apiutil.go
index aa62299..fed6fb5 100644
--- a/srv/src/http/apiutil/apiutil.go
+++ b/srv/src/http/apiutil/apiutil.go
@@ -117,6 +117,9 @@ func RandStr(numBytes int) string {
//
// If no Handler is defined for a method then a 405 Method Not Allowed error is
// returned.
+//
+// If the method "*" is defined then all methods not defined will be directed to
+// that handler, and 405 Method Not Allowed is never returned.
func MethodMux(handlers map[string]http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
@@ -128,13 +131,16 @@ func MethodMux(handlers map[string]http.Handler) http.Handler {
method = formMethod
}
- handler, ok := handlers[method]
+ if handler, ok := handlers[method]; ok {
+ handler.ServeHTTP(rw, r)
+ return
+ }
- if !ok {
- http.Error(rw, "Method not allowed", http.StatusMethodNotAllowed)
+ if handler, ok := handlers["*"]; ok {
+ handler.ServeHTTP(rw, r)
return
}
- handler.ServeHTTP(rw, r)
+ http.Error(rw, "Method not allowed", http.StatusMethodNotAllowed)
})
}