render error page on 502 proxy status (#574)

Co-authored-by: Amnay Mokhtari <amnay.mokhtari@adevinta.com>
This commit is contained in:
Amnay
2020-05-24 21:09:00 +01:00
committed by GitHub
co-authored by Amnay Mokhtari
parent 810a9e9967
commit 0c9795a964
2 changed files with 20 additions and 0 deletions
+19
View File
@@ -154,9 +154,28 @@ func NewReverseProxy(target *url.URL, opts *options.Options) (proxy *httputil.Re
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
setProxyErrorHandler(proxy, opts)
return proxy
}
func setProxyErrorHandler(proxy *httputil.ReverseProxy, opts *options.Options) {
templates := loadTemplates(opts.CustomTemplatesDir)
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, proxyErr error) {
logger.Printf("Error proxying to upstream server: %v", proxyErr)
w.WriteHeader(http.StatusBadGateway)
data := struct {
Title string
Message string
ProxyPrefix string
}{
Title: "Bad Gateway",
Message: "Error proxying to upstream server",
ProxyPrefix: opts.ProxyPrefix,
}
templates.ExecuteTemplate(w, "error.html", data)
}
}
func setProxyUpstreamHostHeader(proxy *httputil.ReverseProxy, target *url.URL) {
director := proxy.Director
proxy.Director = func(req *http.Request) {