Improved layout middleware

This commit is contained in:
2019-06-03 14:53:04 +09:00
parent 3268488b7b
commit cf68184c46
6 changed files with 24 additions and 18 deletions

View File

@ -2,7 +2,6 @@ package middleware
import (
"sort"
"strings"
"github.com/aerogo/aero"
"github.com/akyoto/stringutils/unsafe"
@ -14,10 +13,6 @@ import (
// to be wrapped around the general layout.
func Layout(next aero.Handler) aero.Handler {
return func(ctx aero.Context) error {
if ctx.Request().Method() != "GET" || !strings.Contains(ctx.Request().Header("Accept"), "text/html") || strings.HasPrefix(ctx.Path(), "/_") || strings.HasPrefix(ctx.Path(), "/api/") || strings.HasPrefix(ctx.Path(), "/graphql") {
return next(ctx)
}
ctx.AddModifier(func(content []byte) []byte {
user := arn.GetUserFromContext(ctx)
customCtx := ctx.(*OpenGraphContext)
@ -42,6 +37,9 @@ func Layout(next aero.Handler) aero.Handler {
sort.Strings(tags)
}
// Assure that errors are formatted as HTML
ctx.Response().SetHeader("Content-Type", "text/html; charset=utf-8")
html := components.Layout(ctx, user, openGraph, meta, tags, unsafe.BytesToString(content))
return unsafe.StringToBytes(html)
})

View File

@ -3,7 +3,9 @@ package middleware
import (
"fmt"
"net/http"
"os"
"runtime"
"strings"
"github.com/aerogo/aero"
)
@ -26,7 +28,11 @@ func Recover(next aero.Handler) aero.Handler {
stack := make([]byte, 4096)
length := runtime.Stack(stack, true)
_ = ctx.Error(http.StatusInternalServerError, err, stack[:length])
stackString := string(stack[:length])
fmt.Fprint(os.Stderr, stackString)
message := err.Error() + "<br><br>" + strings.ReplaceAll(stackString, "\n", "<br>")
_ = ctx.Error(http.StatusInternalServerError, message)
}()
return next(ctx)