Skip to content
Snippets Groups Projects
Commit 1a00a55c authored by Alex Ellis (OpenFaaS Ltd)'s avatar Alex Ellis (OpenFaaS Ltd) Committed by Alex Ellis
Browse files

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: default avatarAlex Ellis (OpenFaaS Ltd) <alex@openfaas.com>
parent bc2eeff4
No related branches found
No related tags found
No related merge requests found
module github.com/openfaas/faas/gateway
go 1.17
go 1.18
require (
github.com/docker/distribution v2.8.1+incompatible
......
......@@ -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()
url := r.URL.String()
writer := newWriteInterceptor(w)
next(&writer, r)
writer := httputil.NewHttpWriteInterceptor(w)
next(writer, r)
url := r.URL.String()
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)
}
package httputil
import (
"bufio"
"net"
"net/http"
)
func NewHttpWriteInterceptor(w http.ResponseWriter) *HttpWriteInterceptor {
return &HttpWriteInterceptor{w, 0}
}
type HttpWriteInterceptor struct {
http.ResponseWriter
statusCode int
}
func (c *HttpWriteInterceptor) Status() int {
if c.statusCode == 0 {
return http.StatusOK
}
return c.statusCode
}
func (c *HttpWriteInterceptor) Header() http.Header {
return c.ResponseWriter.Header()
}
func (c *HttpWriteInterceptor) Write(data []byte) (int, error) {
if c.statusCode == 0 {
c.WriteHeader(http.StatusOK)
}
return c.ResponseWriter.Write(data)
}
func (c *HttpWriteInterceptor) WriteHeader(code int) {
c.statusCode = code
c.ResponseWriter.WriteHeader(code)
}
func (c *HttpWriteInterceptor) Flush() {
fl := c.ResponseWriter.(http.Flusher)
fl.Flush()
}
func (c *HttpWriteInterceptor) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hj := c.ResponseWriter.(http.Hijacker)
return hj.Hijack()
}
func (c *HttpWriteInterceptor) CloseNotify() <-chan bool {
notifier, ok := c.ResponseWriter.(http.CloseNotifier)
if ok == false {
return nil
}
return notifier.CloseNotify()
}
package httputil
import (
"fmt"
"net/http"
)
// Errorf sets the response status code and write formats the provided message as the
// response body
func Errorf(w http.ResponseWriter, statusCode int, msg string, args ...interface{}) {
http.Error(w, fmt.Sprintf(msg, args...), statusCode)
}
......@@ -40,6 +40,7 @@ github.com/nats-io/stan.go/pb
# github.com/openfaas/faas-provider v0.19.1
## explicit; go 1.17
github.com/openfaas/faas-provider/auth
github.com/openfaas/faas-provider/httputil
github.com/openfaas/faas-provider/types
# github.com/openfaas/nats-queue-worker v0.0.0-20220805080536-d1d72d857b1c
## explicit; go 1.16
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment