2020-05-26 20:53:10 +02:00
|
|
|
package upstream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultStaticResponseCode = 200
|
|
|
|
|
|
|
|
// newStaticResponseHandler creates a new staticResponseHandler that serves a
|
|
|
|
// a static response code.
|
|
|
|
func newStaticResponseHandler(upstream string, code *int) http.Handler {
|
|
|
|
return &staticResponseHandler{
|
2020-08-31 12:22:10 +02:00
|
|
|
code: derefStaticCode(code),
|
2020-05-26 20:53:10 +02:00
|
|
|
upstream: upstream,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// staticResponseHandler responds with a static response with the given response code.
|
|
|
|
type staticResponseHandler struct {
|
|
|
|
code int
|
|
|
|
upstream string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP serves a static response.
|
|
|
|
func (s *staticResponseHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
rw.Header().Set("GAP-Upstream-Address", s.upstream)
|
|
|
|
rw.WriteHeader(s.code)
|
|
|
|
fmt.Fprintf(rw, "Authenticated")
|
|
|
|
}
|
2020-08-31 12:22:10 +02:00
|
|
|
|
|
|
|
// derefStaticCode returns the derefenced value, or the default if the value is nil
|
|
|
|
func derefStaticCode(code *int) int {
|
|
|
|
if code != nil {
|
|
|
|
return *code
|
|
|
|
}
|
|
|
|
return defaultStaticResponseCode
|
|
|
|
}
|