JSON feed support for the blog cuz why not

This commit is contained in:
Noah 2019-08-19 18:30:42 -07:00
parent 82b73f26b4
commit 445fffdf2b
3 changed files with 8 additions and 1 deletions

View File

@ -70,6 +70,7 @@ func feedHandler(w http.ResponseWriter, r *http.Request) {
} }
feed.Items = append(feed.Items, &feeds.Item{ feed.Items = append(feed.Items, &feeds.Item{
Id: fmt.Sprintf("%d", p.ID),
Title: p.Title, Title: p.Title,
Link: &feeds.Link{Href: config.Site.URL + "/" + p.Fragment}, Link: &feeds.Link{Href: config.Site.URL + "/" + p.Fragment},
Description: rendered, Description: rendered,
@ -83,8 +84,12 @@ func feedHandler(w http.ResponseWriter, r *http.Request) {
// What format to encode it in? // What format to encode it in?
if strings.Contains(r.URL.Path, ".atom") { if strings.Contains(r.URL.Path, ".atom") {
atom, _ := feed.ToAtom() atom, _ := feed.ToAtom()
w.Header().Set("Content-Type", "application/atom+xml") w.Header().Set("Content-Type", "application/atom+xml; encoding=utf-8")
w.Write([]byte(atom)) w.Write([]byte(atom))
} else if strings.Contains(r.URL.Path, ".json") {
jsonData, _ := feed.ToJSON()
w.Header().Set("Content-Type", "application/json; encoding=utf-8")
w.Write([]byte(jsonData))
} else { } else {
rss, _ := feed.ToRss() rss, _ := feed.ToRss()
w.Header().Set("Content-Type", "application/rss+xml; encoding=utf-8") w.Header().Set("Content-Type", "application/rss+xml; encoding=utf-8")

View File

@ -48,6 +48,7 @@ func Register(r *mux.Router, loginError http.HandlerFunc) {
r.HandleFunc("/blog", indexHandler) r.HandleFunc("/blog", indexHandler)
r.HandleFunc("/blog.rss", feedHandler) r.HandleFunc("/blog.rss", feedHandler)
r.HandleFunc("/blog.atom", feedHandler) r.HandleFunc("/blog.atom", feedHandler)
r.HandleFunc("/blog.json", feedHandler)
r.HandleFunc("/archive", archiveHandler) r.HandleFunc("/archive", archiveHandler)
r.HandleFunc("/tagged", taggedHandler) r.HandleFunc("/tagged", taggedHandler)
r.HandleFunc("/tagged/{tag}", taggedHandler) r.HandleFunc("/tagged/{tag}", taggedHandler)

View File

@ -13,6 +13,7 @@ import (
var ageGateSuffixes = []string{ var ageGateSuffixes = []string{
"/blog.rss", // Allow public access to RSS and Atom feeds. "/blog.rss", // Allow public access to RSS and Atom feeds.
"/blog.atom", "/blog.atom",
"/blog.json",
".js", ".js",
".css", ".css",
".txt", ".txt",