outposts/proxy: make templates more re-usable
Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
@ -2,33 +2,38 @@ package application
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// NewProxyErrorHandler creates a ProxyErrorHandler using the template given.
|
||||
func (a *Application) newProxyErrorHandler(errorTemplate *template.Template) func(http.ResponseWriter, *http.Request, error) {
|
||||
return func(rw http.ResponseWriter, req *http.Request, proxyErr error) {
|
||||
claims, _ := a.getClaims(req)
|
||||
log.WithError(proxyErr).Warning("Error proxying to upstream server")
|
||||
rw.WriteHeader(http.StatusBadGateway)
|
||||
data := struct {
|
||||
Title string
|
||||
Message string
|
||||
ProxyPrefix string
|
||||
}{
|
||||
Title: "Bad Gateway",
|
||||
Message: "Error proxying to upstream server",
|
||||
ProxyPrefix: "/akprox",
|
||||
}
|
||||
if claims != nil {
|
||||
data.Message = fmt.Sprintf("Error proxying to upstream server: %s", proxyErr.Error())
|
||||
}
|
||||
err := errorTemplate.Execute(rw, data)
|
||||
if err != nil {
|
||||
http.Error(rw, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
type ErrorPageData struct {
|
||||
Title string
|
||||
Message string
|
||||
ProxyPrefix string
|
||||
}
|
||||
|
||||
func (a *Application) ErrorPage(rw http.ResponseWriter, r *http.Request, err string) {
|
||||
claims, _ := a.getClaims(r)
|
||||
data := ErrorPageData{
|
||||
Title: "Bad Gateway",
|
||||
Message: "Error proxying to upstream server",
|
||||
ProxyPrefix: "/akprox",
|
||||
}
|
||||
if claims != nil && len(err) > 0 {
|
||||
data.Message = err
|
||||
}
|
||||
er := a.errorTemplates.Execute(rw, data)
|
||||
if er != nil {
|
||||
http.Error(rw, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
// NewProxyErrorHandler creates a ProxyErrorHandler using the template given.
|
||||
func (a *Application) newProxyErrorHandler() func(http.ResponseWriter, *http.Request, error) {
|
||||
return func(rw http.ResponseWriter, req *http.Request, proxyErr error) {
|
||||
log.WithError(proxyErr).Warning("Error proxying to upstream server")
|
||||
rw.WriteHeader(http.StatusBadGateway)
|
||||
a.ErrorPage(rw, req, fmt.Sprintf("Error proxying to upstream server: %s", proxyErr.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user