added multihandler and statushandler
This commit is contained in:
parent
a2c4a784ef
commit
6c3226ff63
@ -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
|
||||
}
|
||||
|
21
pkg/http/status_handler.go
Normal file
21
pkg/http/status_handler.go
Normal 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)
|
||||
)
|
Loading…
Reference in New Issue
Block a user