Files
sm-sitemap-plugin/classes/settings.go
Jakub Zych 3359f1a196 feat: sitemap index and per-type files from a slot table (tracer)
- contract package: Filter, Listing and their types (D-95)
- golem15_sitemap_urls slot table and the new settings columns
- Rebuild over signpost Records, stable slot assignment, rebuild job
- GET /sitemap.xml index and GET /_sitemap/files/{file} from the table
2026-11-04 10:58:05 +01:00

62 lines
1.9 KiB
Go

package classes
import (
"strings"
"git.golem15.com/golem15/sm-sitemap-plugin/models"
"git.golem15.com/golem15/summercms/modules/backpack"
"gorm.io/gorm"
)
// SiteURLConfigKey is the config key of the site address the sitemaps use
// when the setting is empty.
const SiteURLConfigKey = "golem15.sitemap.site_url"
// LoadSettings reads the settings row; without one the defaults apply.
func LoadSettings(db *gorm.DB) (models.Settings, error) {
var rows []models.Settings
if err := db.Where("id = ?", 1).Limit(1).Find(&rows).Error; err != nil {
return models.Settings{}, err
}
if len(rows) == 0 {
return models.DefaultSettings(), nil
}
return rows[0], nil
}
// SiteURL is the absolute site address every <loc> starts with, without a
// trailing slash: the site_url setting, else the config key
// golem15.sitemap.site_url, else app.url (CONTEXT 2026-10-28). A value that
// is not an absolute http(s) URL (models.AllowedFrontendURL) is skipped.
// The request's Host header is never used; "" means no site address is
// configured.
func SiteURL(app *backpack.App, s models.Settings) string {
candidates := []string{models.Text(s.SiteURL)}
if app != nil && app.Config != nil {
candidates = append(candidates, app.Config.String(SiteURLConfigKey), app.Config.String("app.url"))
}
for _, c := range candidates {
c = strings.TrimRight(strings.TrimSpace(c), "/")
if c != "" && models.AllowedFrontendURL(c) {
return c
}
}
return ""
}
// Absolute turns a stored address into the absolute URL of a <loc>: a path
// starting with a single / is put on site; an absolute http(s) URL is kept.
// Anything else, or a path without a site address, is "".
func Absolute(site, u string) string {
switch {
case strings.HasPrefix(u, "/") && !strings.HasPrefix(u, "//"):
if site == "" {
return ""
}
return site + u
case models.AllowedFrontendURL(u):
return u
}
return ""
}