Move API handlers to separate package

This makes the package split a little cleaner and easier to import the
HTTP handlers for other consumers.
This commit is contained in:
Brian Goff
2018-09-18 10:05:54 -07:00
parent 74f76c75d5
commit da5e24ef4d
6 changed files with 197 additions and 150 deletions

31
vkubelet/api/helpers.go Normal file
View File

@@ -0,0 +1,31 @@
package api
import (
"io"
"net/http"
"github.com/cpuguy83/strongerrors/status"
"github.com/virtual-kubelet/virtual-kubelet/log"
)
type handlerFunc func(http.ResponseWriter, *http.Request) error
func handleError(f handlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
err := f(w, req)
if err == nil {
return
}
code, _ := status.HTTPCode(err)
w.WriteHeader(code)
io.WriteString(w, err.Error())
logger := log.G(req.Context()).WithError(err).WithField("httpStatusCode", code)
if code >= 500 {
logger.Error("Internal server error on request")
} else {
log.Trace(logger, "Error on request")
}
}
}