mirror of
https://github.com/openfaas/faas.git
synced 2025-06-09 16:56:47 +00:00
These two functions are effectively the same, with the former being a wrapper for the later. Signed-off-by: Alex Ellis <alexellis2@gmail.com>
38 lines
815 B
Go
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)
|
|
}
|
|
}
|