Added a sitemap

This commit is contained in:
Eduard Urbach 2025-03-01 12:32:16 +01:00
parent f8464eabf3
commit ee6ab6f00d
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
2 changed files with 39 additions and 0 deletions

View File

@ -22,5 +22,6 @@ func main() {
server.Get("/", pages.Frontpage) server.Get("/", pages.Frontpage)
server.Get("/blog", pages.Blog) server.Get("/blog", pages.Blog)
server.Get("/:post", pages.Post) server.Get("/:post", pages.Post)
server.Get("/sitemap.txt", pages.Sitemap)
server.Run(address) server.Run(address)
} }

38
server/pages/Sitemap.go Normal file
View File

@ -0,0 +1,38 @@
package pages
import (
"bytes"
"sort"
"git.urbach.dev/go/web"
"git.urbach.dev/go/web/send"
"git.urbach.dev/web/urbach.dev/server/app"
)
func Sitemap(ctx web.Context) error {
html := bytes.Buffer{}
html.WriteString("https://urbach.dev\n")
html.WriteString("https://urbach.dev/blog\n")
articles := []*app.Post{}
for _, post := range app.Posts {
if !post.Published {
continue
}
articles = append(articles, post)
}
sort.Slice(articles, func(i, j int) bool {
return articles[i].Created > articles[j].Created
})
for _, post := range articles {
html.WriteString("https://urbach.dev/")
html.WriteString(post.Slug)
html.WriteByte('\n')
}
return send.Text(ctx, html.String())
}