added multihandler and statushandler

This commit is contained in:
Colin Henry 2020-06-14 20:12:32 -07:00
parent a2c4a784ef
commit 6c3226ff63
2 changed files with 27 additions and 1 deletions

View File

@ -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
}

View File

@ -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)
)