WIP, but this is going to become my new blog CMS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

39 lines
779 B

package main
import (
"net/http"
"time"
"github.com/araddon/dateparse"
"github.com/snabb/sitemap"
)
const sitemapPath = "/sitemap.xml"
func serveSitemap(w http.ResponseWriter, r *http.Request) {
posts, err := getPosts(&postsRequestConfig{
status: statusPublished,
})
if err != nil {
serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
sm := sitemap.New()
sm.Minify = true
for _, p := range posts {
item := &sitemap.URL{
Loc: p.fullURL()}
var lastMod time.Time
if p.Updated != "" {
lastMod, _ = dateparse.ParseLocal(p.Updated)
}
if p.Published != "" && lastMod.IsZero() {
lastMod, _ = dateparse.ParseLocal(p.Published)
}
if !lastMod.IsZero() {
item.LastMod = &lastMod
}
sm.Add(item)
}
_, _ = sm.WriteTo(w)
}