mirror of
https://github.com/openfaas/faas.git
synced 2025-06-19 20:46:41 +00:00
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:
committed by
Alex Ellis
parent
bc2eeff467
commit
1a00a55c77
@ -1,6 +1,6 @@
|
||||
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()
|
||||
|
||||
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)
|
||||
}
|
||||
|
57
gateway/vendor/github.com/openfaas/faas-provider/httputil/write_interceptor.go
generated
vendored
Normal file
57
gateway/vendor/github.com/openfaas/faas-provider/httputil/write_interceptor.go
generated
vendored
Normal file
@ -0,0 +1,57 @@
|
||||
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()
|
||||
}
|
12
gateway/vendor/github.com/openfaas/faas-provider/httputil/writers.go
generated
vendored
Normal file
12
gateway/vendor/github.com/openfaas/faas-provider/httputil/writers.go
generated
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
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)
|
||||
}
|
1
gateway/vendor/modules.txt
vendored
1
gateway/vendor/modules.txt
vendored
@ -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
|
||||
|
Reference in New Issue
Block a user