diff options
| author | 2026-03-31 00:54:20 +0700 | |
|---|---|---|
| committer | 2026-03-31 00:54:20 +0700 | |
| commit | abc9e2fc1e93fba0b274f31b95340c411a73dd6d (patch) | |
| tree | 3d0b70016ce8f160203629da07c1a8ae3edff8af /internal/router/handlers/sitemap.xml.go | |
| parent | 8d0385546b0a5f4f3b0832fce1b4c0a10fa0a927 (diff) | |
| parent | a8f566cbeb8358aca7fb4532d8b69c513ef197f3 (diff) | |
| download | web-0.13.0.tar.gz web-0.13.0.zip | |
v0.13.0 (#25)v0.13.0
- [feat: enable etag
middleware](https://github.com/SayaAndy/saya-today-web/commit/c7742b341a46360d16dd9035c046f004d0fe760c)
- feat: enable compress middleware
- feat: trim end slash of each request url (except for root)
- [feat: add linked
data](https://github.com/SayaAndy/saya-today-web/commit/a8f566cbeb8358aca7fb4532d8b69c513ef197f3)
- [feat: build
sitemap.xml](https://github.com/SayaAndy/saya-today-web/commit/7a40c01dd14f04787f5052d0c89e0d6fb9ce597f)
- [feat: generate
robots.txt](https://github.com/SayaAndy/saya-today-web/commit/be906e1cc4ec3e92bcb14ee08256b53f3a4b44e2)
- [feat: specify canonical hrefs for each
page](https://github.com/SayaAndy/saya-today-web/commit/a0ace41ad3b0f4661e98d8fa8ef254369cc80d1e)
- [feat: use semantic html tags for
pages](https://github.com/SayaAndy/saya-today-web/commit/2bcf8ee39555ea98be309b85547c797b0d172b84)
- feat: specify more meta properties for search engines
- fix: correct user and catalogue meta
- format: specify og property names in property field
- feat: user profile description
Diffstat (limited to 'internal/router/handlers/sitemap.xml.go')
| -rw-r--r-- | internal/router/handlers/sitemap.xml.go | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/internal/router/handlers/sitemap.xml.go b/internal/router/handlers/sitemap.xml.go new file mode 100644 index 0000000..bb1df93 --- /dev/null +++ b/internal/router/handlers/sitemap.xml.go @@ -0,0 +1,79 @@ +package handlers + +import ( + "log/slog" + "time" + + "github.com/SayaAndy/saya-today-web/internal/router" + "github.com/gofiber/fiber/v2" +) + +type SitemapXmlHandler struct { + router.BasicHandler +} + +func init() { + router.Routes = append(router.Routes, &SitemapXmlHandler{}) +} + +func (r *SitemapXmlHandler) Filter() (method string, path string) { + return "GET", "/sitemap.xml" +} + +func (r *SitemapXmlHandler) IsTemplated() bool { + return false +} + +func (r *SitemapXmlHandler) TemplatesToInject() []string { + return []string{"views/pages/sitemap.xml"} +} + +func (r *SitemapXmlHandler) ToCache() router.CacheSetting { + return router.ByUrlOnly +} + +func (r *SitemapXmlHandler) CacheDuration() time.Duration { + return time.Hour +} + +func (r *SitemapXmlHandler) ToValidateLang() router.LangSetting { + return router.NotRequired +} + +func (r *SitemapXmlHandler) ContentType() string { + return fiber.MIMEApplicationXMLCharsetUTF8 +} + +func (r *SitemapXmlHandler) Render(c *fiber.Ctx, supplements *router.Supplements, lang string, templateMap fiber.Map) (statusCode int, err error) { + urls := []router.SitemapInfo{} + for _, route := range router.Routes { + sitemapInfo := route.SitemapInfo(supplements) + if sitemapInfo == nil { + continue + } + + method, match := route.Filter() + lastMod, err := supplements.TemplateManager.GetLastModified(method + " " + match) + if err != nil { + slog.Warn("failed to get last modified for a route", + slog.String("error", err.Error()), + slog.String("method", method), + slog.String("match", match)) + lastMod = time.Now() + } + + for i := range sitemapInfo { + if sitemapInfo[i].LastModified.Before(lastMod) { + sitemapInfo[i].LastModified = lastMod + } + if sitemapInfo[i].ChangeFreq == "" { + sitemapInfo[i].ChangeFreq = "monthly" + } + urls = append(urls, sitemapInfo[i]) + } + } + + templateMap["URLs"] = urls + + return fiber.StatusOK, nil +} |