small pixel drawing of a pufferfish j3s.sh

atom/atom.go

package atom

import (
	"fmt"
	"log"
	"net/http"

	"git.j3s.sh/j3s.sh/thought"
)

const maxFeed = 10

func Handler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/atom+xml")

	posts, err := thought.Posts()
	if err != nil {
		log.Println(err)
		return
	}
	thought.DateSort(posts)

	if len(posts) > maxFeed {
		posts = posts[:maxFeed]
	}

	var updated string
	if len(posts) > 0 {
		updated = posts[0].Updated
	}

	fmt.Fprintf(w, `<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title>j3s.sh</title>
  <link rel="self" href="https://j3s.sh/feed.atom" />
  <link href="https://j3s.sh/" />
  <updated>%s</updated>
  <author>
    <name>Jes Olson</name>
  </author>
  <id>https://j3s.sh/</id>
`, updated)

	// maybe add p.Content here someday
	for _, p := range posts {
		fmt.Fprintf(w, `
  <entry>
    <title>%s</title>
    <link href="%s"/>
    <id>%s</id>
    <updated>%s</updated>
  </entry>
`, p.Title, p.Link, p.Link, p.Updated)
	}
	fmt.Fprint(w, `
</feed>`)
}