faas/gateway/handlers/external_auth.go
Alex Ellis c9b5e5f146 Move to use WithTimeout instead of WithDeadline for context
These two functions are effectively the same, with the former
being a wrapper for the later.

Signed-off-by: Alex Ellis <alexellis2@gmail.com>
2019-06-05 18:13:49 +01:00

38 lines
815 B
Go

package handlers
import (
"context"
"net/http"
"time"
)
// MakeExternalAuthHandler make an authentication proxy handler
func MakeExternalAuthHandler(next http.HandlerFunc, upstreamTimeout time.Duration, upstreamURL string, passBody bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
req, _ := http.NewRequest(http.MethodGet, upstreamURL, nil)
deadlineContext, cancel := context.WithTimeout(
context.Background(),
upstreamTimeout)
defer cancel()
res, err := http.DefaultClient.Do(req.WithContext(deadlineContext))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if res.Body != nil {
defer res.Body.Close()
}
if res.StatusCode == http.StatusOK {
next.ServeHTTP(w, r)
return
}
w.WriteHeader(res.StatusCode)
}
}