fix(17): WR-05 throttle /sitemap.xml and cache its output
The anonymous route resolved every registry item on each request with no throttle and no cache. - surf bucket sitemap-xml: 60 requests a minute per client IP. - The generated XML is cached for golem15.sitemap.cache_seconds (default 60, 0 off), keyed by root URL, definition and settings so a save shows at once; one generation runs at a time.
This commit is contained in:
26
plugin.go
26
plugin.go
@@ -8,6 +8,7 @@ import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"time"
|
||||
|
||||
"git.golem15.com/golem15/sm-sitemap-plugin/classes"
|
||||
"git.golem15.com/golem15/sm-sitemap-plugin/console"
|
||||
@@ -17,16 +18,18 @@ import (
|
||||
"git.golem15.com/golem15/summercms/modules/bonfire"
|
||||
"git.golem15.com/golem15/summercms/modules/pact"
|
||||
"git.golem15.com/golem15/summercms/modules/party"
|
||||
"git.golem15.com/golem15/summercms/modules/surf"
|
||||
"github.com/go-gormigrate/gormigrate/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
_ party.Plugin = (*Plugin)(nil)
|
||||
_ pact.HasConfig = (*Plugin)(nil)
|
||||
_ pact.HasMigrations = (*Plugin)(nil)
|
||||
_ pact.HasModels = (*Plugin)(nil)
|
||||
_ pact.HasRoutes = (*Plugin)(nil)
|
||||
_ pact.HasCommands = (*Plugin)(nil)
|
||||
_ party.Plugin = (*Plugin)(nil)
|
||||
_ pact.HasConfig = (*Plugin)(nil)
|
||||
_ pact.HasMigrations = (*Plugin)(nil)
|
||||
_ pact.HasModels = (*Plugin)(nil)
|
||||
_ pact.HasRoutes = (*Plugin)(nil)
|
||||
_ pact.HasCommands = (*Plugin)(nil)
|
||||
_ surf.BucketProvider = (*Plugin)(nil)
|
||||
)
|
||||
|
||||
//go:embed config
|
||||
@@ -41,6 +44,7 @@ var sitemapXSL []byte
|
||||
type Plugin struct {
|
||||
app *backpack.App
|
||||
tiers classes.Tiers
|
||||
cache *sitemapCache
|
||||
}
|
||||
|
||||
func (p *Plugin) ID() string { return "golem15.sitemap" }
|
||||
@@ -56,17 +60,27 @@ func (p *Plugin) Register(app *backpack.App) error {
|
||||
|
||||
// Boot compiles the priority tier rules (classes.TiersConfigKey); a rule
|
||||
// with an unknown setting or a pattern that does not compile fails the boot.
|
||||
// It also starts an empty sitemap cache for CacheConfigKey seconds; a
|
||||
// negative value fails the boot.
|
||||
func (p *Plugin) Boot(app *backpack.App) error {
|
||||
p.app = app
|
||||
var tiers classes.Tiers
|
||||
var err error
|
||||
seconds := 0
|
||||
if app != nil {
|
||||
tiers, err = classes.LoadTiers(app.Config)
|
||||
if app.Config != nil {
|
||||
seconds = app.Config.Int(CacheConfigKey)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("golem15.sitemap: %w", err)
|
||||
}
|
||||
if seconds < 0 {
|
||||
return fmt.Errorf("golem15.sitemap: %s must not be negative, got %d", CacheConfigKey, seconds)
|
||||
}
|
||||
p.tiers = tiers
|
||||
p.cache = newSitemapCache(time.Duration(seconds) * time.Second)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user