2020-09-22 15:15:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
2020-10-15 15:32:46 +00:00
|
|
|
|
|
|
|
"github.com/araddon/dateparse"
|
|
|
|
"github.com/snabb/sitemap"
|
2020-09-22 15:15:17 +00:00
|
|
|
)
|
|
|
|
|
2020-10-15 17:50:34 +00:00
|
|
|
const sitemapPath = "/sitemap.xml"
|
|
|
|
|
|
|
|
func serveSitemap(w http.ResponseWriter, r *http.Request) {
|
2020-10-19 18:25:30 +00:00
|
|
|
posts, err := getPosts(&postsRequestConfig{})
|
2020-10-15 17:50:34 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
sm := sitemap.New()
|
|
|
|
sm.Minify = true
|
|
|
|
for _, p := range posts {
|
|
|
|
item := &sitemap.URL{
|
|
|
|
Loc: appConfig.Server.PublicAddress + p.Path}
|
|
|
|
var lastMod time.Time
|
|
|
|
if p.Updated != "" {
|
|
|
|
lastMod, _ = dateparse.ParseIn(p.Updated, time.Local)
|
|
|
|
}
|
|
|
|
if p.Published != "" && lastMod.IsZero() {
|
|
|
|
lastMod, _ = dateparse.ParseIn(p.Published, time.Local)
|
2020-09-22 15:15:17 +00:00
|
|
|
}
|
2020-10-15 17:50:34 +00:00
|
|
|
if !lastMod.IsZero() {
|
|
|
|
item.LastMod = &lastMod
|
2020-09-22 15:15:17 +00:00
|
|
|
}
|
2020-10-15 17:50:34 +00:00
|
|
|
sm.Add(item)
|
2020-09-22 15:15:17 +00:00
|
|
|
}
|
2020-10-15 17:50:34 +00:00
|
|
|
_, _ = sm.WriteTo(w)
|
2020-09-22 15:15:17 +00:00
|
|
|
}
|