From 1de0ab3b720cf7b83a7e29de4dbe35c117ccea0e Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 21 May 2022 09:17:43 -0600 Subject: Define an actual middleware type, use that to set up API routes --- srv/src/http/apiutil/apiutil.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'srv/src/http/apiutil/apiutil.go') 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) }) } -- cgit v1.2.3