diff --git a/main.go b/main.go index 0af6d6f..d7351cc 100644 --- a/main.go +++ b/main.go @@ -22,5 +22,6 @@ func main() { server.Get("/", pages.Frontpage) server.Get("/blog", pages.Blog) server.Get("/:post", pages.Post) + server.Get("/sitemap.txt", pages.Sitemap) server.Run(address) } diff --git a/server/pages/Sitemap.go b/server/pages/Sitemap.go new file mode 100644 index 0000000..efbe9ac --- /dev/null +++ b/server/pages/Sitemap.go @@ -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()) +}