2019-06-03 03:20:17 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/aerogo/aero"
|
|
|
|
"github.com/akyoto/stringutils/unsafe"
|
2019-06-03 09:32:43 +00:00
|
|
|
"github.com/animenotifier/notify.moe/arn"
|
2019-06-03 03:20:17 +00:00
|
|
|
"github.com/animenotifier/notify.moe/components"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Layout middleware modifies the response body
|
|
|
|
// to be wrapped around the general layout.
|
|
|
|
func Layout(next aero.Handler) aero.Handler {
|
|
|
|
return func(ctx aero.Context) error {
|
|
|
|
ctx.AddModifier(func(content []byte) []byte {
|
|
|
|
user := arn.GetUserFromContext(ctx)
|
|
|
|
customCtx := ctx.(*OpenGraphContext)
|
|
|
|
openGraph := customCtx.OpenGraph
|
|
|
|
|
|
|
|
// Make output order deterministic to profit from Aero caching.
|
|
|
|
// To do this, we need to create slices and sort the tags.
|
|
|
|
var meta []string
|
|
|
|
var tags []string
|
|
|
|
|
|
|
|
if openGraph != nil {
|
|
|
|
for name := range openGraph.Meta {
|
|
|
|
meta = append(meta, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(meta)
|
|
|
|
|
|
|
|
for name := range openGraph.Tags {
|
|
|
|
tags = append(tags, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(tags)
|
|
|
|
}
|
|
|
|
|
2019-06-03 05:53:04 +00:00
|
|
|
// Assure that errors are formatted as HTML
|
|
|
|
ctx.Response().SetHeader("Content-Type", "text/html; charset=utf-8")
|
|
|
|
|
2019-06-03 03:20:17 +00:00
|
|
|
html := components.Layout(ctx, user, openGraph, meta, tags, unsafe.BytesToString(content))
|
|
|
|
return unsafe.StringToBytes(html)
|
|
|
|
})
|
|
|
|
|
|
|
|
return next(ctx)
|
|
|
|
}
|
|
|
|
}
|