Use write interceptor from faas-provider

We now have two write interceptors, with one moved into
faas-provider. This commit makes the gateway use the new
external package and deletes its own.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alex@openfaas.com>
This commit is contained in:
Alex Ellis (OpenFaaS Ltd)
2022-09-29 18:11:30 +01:00
committed by Alex Ellis
parent bc2eeff467
commit 1a00a55c77
5 changed files with 77 additions and 36 deletions

View File

@ -6,50 +6,21 @@ package handlers
import (
"net/http"
"time"
"github.com/openfaas/faas-provider/httputil"
)
// MakeNotifierWrapper wraps a http.HandlerFunc in an interceptor to pass to HTTPNotifier
func MakeNotifierWrapper(next http.HandlerFunc, notifiers []HTTPNotifier) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
then := time.Now()
writer := newWriteInterceptor(w)
next(&writer, r)
url := r.URL.String()
writer := httputil.NewHttpWriteInterceptor(w)
next(writer, r)
for _, notifier := range notifiers {
notifier.Notify(r.Method, url, url, writer.Status(), "completed", time.Since(then))
}
}
}
func newWriteInterceptor(w http.ResponseWriter) writeInterceptor {
return writeInterceptor{
w: w,
}
}
type writeInterceptor struct {
CapturedStatusCode int
w http.ResponseWriter
}
func (c *writeInterceptor) Status() int {
if c.CapturedStatusCode == 0 {
return http.StatusOK
}
return c.CapturedStatusCode
}
func (c *writeInterceptor) Header() http.Header {
return c.w.Header()
}
func (c *writeInterceptor) Write(data []byte) (int, error) {
return c.w.Write(data)
}
func (c *writeInterceptor) WriteHeader(code int) {
c.CapturedStatusCode = code
c.w.WriteHeader(code)
}