Improved layout middleware
This commit is contained in:
parent
3268488b7b
commit
cf68184c46
4
go.mod
4
go.mod
@ -5,7 +5,7 @@ go 1.12
|
||||
require (
|
||||
cloud.google.com/go v0.39.0 // indirect
|
||||
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
|
||||
github.com/aerogo/aero v1.3.8
|
||||
github.com/aerogo/aero v1.3.10
|
||||
github.com/aerogo/api v0.2.0
|
||||
github.com/aerogo/crawler v0.2.5
|
||||
github.com/aerogo/graphql v0.4.0
|
||||
@ -52,4 +52,6 @@ require (
|
||||
gopkg.in/yaml.v2 v2.2.2 // indirect
|
||||
)
|
||||
|
||||
replace github.com/aerogo/aero => /home/eduard/projects/aerogo/aero
|
||||
|
||||
exclude github.com/logpacker/PayPal-Go-SDK v2.0.0+incompatible
|
||||
|
8
go.sum
8
go.sum
@ -9,10 +9,6 @@ github.com/PuerkitoBio/goquery v1.5.0 h1:uGvmFXOA73IKluu/F84Xd1tt/z07GYm8X49XKHP
|
||||
github.com/PuerkitoBio/goquery v1.5.0/go.mod h1:qD2PgZ9lccMbQlc7eEOjaeRlFQON7xY8kdmcsrnKqMg=
|
||||
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
|
||||
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
|
||||
github.com/aerogo/aero v1.3.1 h1:P4XS4ePrJWSJwHLXKkX5YjFsoywhWuXf3dPIX5XVAW0=
|
||||
github.com/aerogo/aero v1.3.1/go.mod h1:5rPhXo2DNMFQ7XhDsuZ3L7Zr6TH/349+WczUbrOUZvM=
|
||||
github.com/aerogo/aero v1.3.8 h1:bKYigLZx4NupGb2mu2I0KdvQH2Kq8rP7Xg1scrz10sQ=
|
||||
github.com/aerogo/aero v1.3.8/go.mod h1:DZA+Yt0AevLyvNG7w1CyYoZZfoFMysABOOrFrgrQhHM=
|
||||
github.com/aerogo/api v0.2.0 h1:mIc/y381e+Qc85eSc2cKPdpDDOmT0hlnEeCw2Dcf7no=
|
||||
github.com/aerogo/api v0.2.0/go.mod h1:6objJn5XiKpYpywQUPrFjxZIXD4NVI2LwcBNYCEcS3Y=
|
||||
github.com/aerogo/cluster v0.1.6 h1:9HYjJwo19uuh9thIc80T3caap9t9b4BXZ1iN8aztjlU=
|
||||
@ -71,8 +67,6 @@ github.com/akyoto/color v1.8.5 h1:xvHRmBJdsT5HJxnfyIT8l3zNbexA/2YIIeAlaHuhYGY=
|
||||
github.com/akyoto/color v1.8.5/go.mod h1:mI8lhoKcgnvH8fnaupVUE3BmKUp2bpDn5wFS1xEQ4hw=
|
||||
github.com/akyoto/go-matroska v0.1.1 h1:HgoCAkeWrGjYr0FZr3yCzAIkXuOGRiVil7Ul329lm+A=
|
||||
github.com/akyoto/go-matroska v0.1.1/go.mod h1:x+GUVwyby6HN/MKKNP4BvGqP9VrHuEznfBf288gehek=
|
||||
github.com/akyoto/hash v0.3.5 h1:5EJGHx6RfE9aHrEzWU3pfLGFUWMvPVqtsxt7mSON+mY=
|
||||
github.com/akyoto/hash v0.3.5/go.mod h1:uPmnZyhBJIyLON8V9LNi0CcqtwYaH2RiKLFQg67fwq0=
|
||||
github.com/akyoto/hash v0.4.0 h1:M9Q3E6cOAxbf7q3yu5SZZdbZxC4thndCkqJctxl1+zg=
|
||||
github.com/akyoto/hash v0.4.0/go.mod h1:purxg2OohOWT7H0oLP0xQF1DG/EbazUbw7zomRUk8Y8=
|
||||
github.com/akyoto/imageserver v0.3.6 h1:Sxcbgo45Lh7afcSmcU8OS49VYbqh4kE3DK0Lxuuxf74=
|
||||
@ -280,8 +274,6 @@ golang.org/x/sys v0.0.0-20190516014833-cab07311ab81 h1:5Q88vZAfC0WB8T1GHRLttQaZd
|
||||
golang.org/x/sys v0.0.0-20190516014833-cab07311ab81/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190523142557-0e01d883c5c5 h1:sM3evRHxE/1RuMe1FYAL3j7C7fUfIjkbE+NiDAYUF8U=
|
||||
golang.org/x/sys v0.0.0-20190523142557-0e01d883c5c5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190531175056-4c3a928424d2 h1:T5DasATyLQfmbTpfEXx/IOL9vfjzW6up+ZDkmHvIf2s=
|
||||
golang.org/x/sys v0.0.0-20190531175056-4c3a928424d2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed h1:uPxWBzB3+mlnjy9W58qY1j/cjyFjutgw/Vhan2zLy/A=
|
||||
golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||
|
7
main.go
7
main.go
@ -45,8 +45,8 @@ func configure(app *aero.Application) *aero.Application {
|
||||
|
||||
// Middleware
|
||||
app.Use(
|
||||
middleware.Recover,
|
||||
middleware.OpenGraph,
|
||||
middleware.Layout,
|
||||
middleware.Log,
|
||||
middleware.Session,
|
||||
middleware.UserInfo,
|
||||
@ -70,6 +70,11 @@ func configure(app *aero.Application) *aero.Application {
|
||||
// Close the database node on shutdown
|
||||
app.OnEnd(arn.Node.Close)
|
||||
|
||||
// Don't push when an underscore URL has been requested
|
||||
app.AddPushCondition(func(ctx aero.Context) bool {
|
||||
return !strings.HasPrefix(ctx.Path(), "/_")
|
||||
})
|
||||
|
||||
// Show errors in the console
|
||||
app.OnError(func(ctx aero.Context, err error) {
|
||||
color.Red(err.Error())
|
||||
|
@ -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)
|
||||
})
|
||||
|
@ -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)
|
||||
|
@ -1,9 +1,12 @@
|
||||
package page
|
||||
|
||||
import "github.com/aerogo/aero"
|
||||
import (
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/animenotifier/notify.moe/middleware"
|
||||
)
|
||||
|
||||
// Get registers a layout rendered route and a contents-only route.
|
||||
func Get(app *aero.Application, route string, handler aero.Handler) {
|
||||
app.Get(route, handler)
|
||||
app.Get(route, middleware.Layout(handler))
|
||||
app.Get("/_"+route, handler)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user