Files
sm-sitemap-plugin/plugin.go
Jakub Zych cf5c40e442 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.
2026-10-12 21:38:39 +02:00

105 lines
3.0 KiB
Go

// Package sitemap is the golem15.sitemap plugin: it serves /sitemap.xml
// from the application's one sitemap definition, byte for byte the way the
// WinterCMS Golem15.Sitemap plugin did, with items that are explicit URLs
// or menu item types of the framework's registry.
package sitemap
import (
"embed"
"fmt"
"io/fs"
"time"
"git.golem15.com/golem15/sm-sitemap-plugin/classes"
"git.golem15.com/golem15/sm-sitemap-plugin/console"
"git.golem15.com/golem15/sm-sitemap-plugin/models"
"git.golem15.com/golem15/sm-sitemap-plugin/updates"
"git.golem15.com/golem15/summercms/modules/backpack"
"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)
_ surf.BucketProvider = (*Plugin)(nil)
)
//go:embed config
var configFS embed.FS
// sitemapXSL is the PHP plugin's assets/sitemap.xsl, byte for byte.
//
//go:embed assets/sitemap.xsl
var sitemapXSL []byte
// Plugin is the golem15.sitemap plugin.
type Plugin struct {
app *backpack.App
tiers classes.Tiers
cache *sitemapCache
}
func (p *Plugin) ID() string { return "golem15.sitemap" }
// Requires is empty: the sitemap reads content types through the framework's
// menu item type registry, not from a named plugin.
func (p *Plugin) Requires() []string { return nil }
func (p *Plugin) Register(app *backpack.App) error {
p.app = app
return nil
}
// 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
}
// ConfigFS ships config/config.yaml: the default priority tier rules.
func (p *Plugin) ConfigFS() fs.FS { return configFS }
// Commands registers sitemap:import and sitemap:import-winter.
func (p *Plugin) Commands() []bonfire.Command {
return []bonfire.Command{
console.Import(p.app),
console.ImportWinter(p.app),
}
}
func (p *Plugin) Migrations() []*gormigrate.Migration { return updates.All() }
func (p *Plugin) Models() []any { return models.All() }
func init() {
party.Register(&Plugin{})
}