From 6c3226ff63e1f85508f55e0db041af740cdebecb Mon Sep 17 00:00:00 2001 From: Colin Henry Date: Sun, 14 Jun 2020 20:12:32 -0700 Subject: [PATCH] added multihandler and statushandler --- pkg/http/multihandler.go | 7 ++++++- pkg/http/status_handler.go | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 pkg/http/status_handler.go diff --git a/pkg/http/multihandler.go b/pkg/http/multihandler.go index 8fd8c52..5dd58eb 100644 --- a/pkg/http/multihandler.go +++ b/pkg/http/multihandler.go @@ -26,6 +26,11 @@ func MutliHandler(h map[string]http.Handler) (http.HandlerFunc, error) { } } return func(w http.ResponseWriter, r *http.Request) { - h[r.Method].ServeHTTP(w, r) + if hdlr, ok := h[r.Method]; ok { + hdlr.ServeHTTP(w, r) + } else { + NotFoundHandler.ServeHTTP(w, r) + } + }, nil } diff --git a/pkg/http/status_handler.go b/pkg/http/status_handler.go new file mode 100644 index 0000000..6db1d37 --- /dev/null +++ b/pkg/http/status_handler.go @@ -0,0 +1,21 @@ +package http + +import ( + "io" + "net/http" +) + +// boosted from @matryer +type StatusHandler int + +func (s StatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + code := int(s) + w.WriteHeader(code) + io.WriteString(w, http.StatusText(code)) +} + +var ( + NotFoundHandler = StatusHandler(404) + NotImplementedHandler = StatusHandler(501) + NotLegalHandler = StatusHandler(451) +)