opt: gzip + no cdn shi + feeds
This commit is contained in:
115
blog/feeds.go
Normal file
115
blog/feeds.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package blog
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const baseURL = "https://chaosmith.systems"
|
||||
|
||||
func generateRSS(s *Store) string {
|
||||
var b strings.Builder
|
||||
b.WriteString(`<?xml version="1.0" encoding="UTF-8"?>` + "\n")
|
||||
b.WriteString(`<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">` + "\n")
|
||||
b.WriteString(" <channel>\n")
|
||||
b.WriteString(" <title>Chaosmith Systems — Field Notes</title>\n")
|
||||
b.WriteString(" <link>" + baseURL + "/blog/</link>\n")
|
||||
b.WriteString(" <description>Engineering, music, and experiments from controlled chaos.</description>\n")
|
||||
b.WriteString(" <language>en</language>\n")
|
||||
b.WriteString(` <atom:link href="` + baseURL + `/blog/feed.xml" rel="self" type="application/rss+xml"/>` + "\n")
|
||||
|
||||
for _, p := range s.All() {
|
||||
b.WriteString(" <item>\n")
|
||||
b.WriteString(" <title>" + xmlEscape(p.Title) + "</title>\n")
|
||||
b.WriteString(" <link>" + baseURL + "/blog/posts/" + p.Slug + "</link>\n")
|
||||
b.WriteString(" <guid>" + baseURL + "/blog/posts/" + p.Slug + "</guid>\n")
|
||||
b.WriteString(" <description>" + xmlEscape(p.Description) + "</description>\n")
|
||||
b.WriteString(" <pubDate>" + p.Date.Format(time.RFC1123Z) + "</pubDate>\n")
|
||||
b.WriteString(" </item>\n")
|
||||
}
|
||||
|
||||
b.WriteString(" </channel>\n</rss>\n")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func generateJSONFeed(s *Store) string {
|
||||
var b strings.Builder
|
||||
b.WriteString(`{"version":"https://jsonfeed.org/version/1.1","title":"Chaosmith Systems — Field Notes","home_page_url":"` + baseURL + `/blog/","feed_url":"` + baseURL + `/blog/feed.json","items":[`)
|
||||
|
||||
first := true
|
||||
for _, p := range s.All() {
|
||||
if !first {
|
||||
b.WriteString(",")
|
||||
}
|
||||
first = false
|
||||
b.WriteString(`{`)
|
||||
b.WriteString(`"id":"` + baseURL + "/blog/posts/" + p.Slug + `",`)
|
||||
b.WriteString(`"url":"` + baseURL + "/blog/posts/" + p.Slug + `",`)
|
||||
b.WriteString(`"title":"` + jsonEscape(p.Title) + `",`)
|
||||
b.WriteString(`"summary":"` + jsonEscape(p.Description) + `",`)
|
||||
b.WriteString(`"date_published":"` + p.Date.Format(time.RFC3339) + `"`)
|
||||
b.WriteString(`}`)
|
||||
}
|
||||
|
||||
b.WriteString("]}")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func generateSitemap(s *Store) string {
|
||||
var b strings.Builder
|
||||
b.WriteString(`<?xml version="1.0" encoding="UTF-8"?>` + "\n")
|
||||
b.WriteString(`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` + "\n")
|
||||
|
||||
// Static pages
|
||||
b.WriteString(" <url>\n")
|
||||
b.WriteString(" <loc>" + baseURL + "/</loc>\n")
|
||||
b.WriteString(" <changefreq>monthly</changefreq>\n")
|
||||
b.WriteString(" </url>\n")
|
||||
b.WriteString(" <url>\n")
|
||||
b.WriteString(" <loc>" + baseURL + "/blog/</loc>\n")
|
||||
b.WriteString(" <changefreq>weekly</changefreq>\n")
|
||||
b.WriteString(" </url>\n")
|
||||
|
||||
for _, p := range s.All() {
|
||||
b.WriteString(" <url>\n")
|
||||
b.WriteString(" <loc>" + baseURL + "/blog/posts/" + p.Slug + "</loc>\n")
|
||||
b.WriteString(" <lastmod>" + p.Date.Format("2006-01-02") + "</lastmod>\n")
|
||||
b.WriteString(" </url>\n")
|
||||
}
|
||||
|
||||
b.WriteString("</urlset>\n")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func xmlEscape(s string) string {
|
||||
var b strings.Builder
|
||||
xml.EscapeText(&b, []byte(s))
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func jsonEscape(s string) string {
|
||||
var b strings.Builder
|
||||
for _, r := range s {
|
||||
switch r {
|
||||
case '"':
|
||||
b.WriteString(`\"`)
|
||||
case '\\':
|
||||
b.WriteString(`\\`)
|
||||
case '\n':
|
||||
b.WriteString(`\n`)
|
||||
case '\r':
|
||||
b.WriteString(`\r`)
|
||||
case '\t':
|
||||
b.WriteString(`\t`)
|
||||
default:
|
||||
if r < 0x20 {
|
||||
b.WriteString(fmt.Sprintf(`\u%04x`, r))
|
||||
} else {
|
||||
b.WriteRune(r)
|
||||
}
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
@@ -34,13 +34,13 @@ const pageTpl = `<!DOCTYPE html>
|
||||
<meta property="og:site_name" content="Chaosmith Systems">
|
||||
<meta name="twitter:card" content="summary">
|
||||
{{end}}
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;600&family=Inter:wght@300;500;700&display=swap" rel="stylesheet">
|
||||
<link rel="stylesheet" href="/src/thirdparty/highlight/default.min.css">
|
||||
<script src="/src/thirdparty/highlight/highlight.min.js"></script>
|
||||
<script src="/src/lang/ergon.js"></script>
|
||||
<script src="/src/lang/archasm.js"></script>
|
||||
<link rel="stylesheet" href="/src/thirdparty/fonts/fonts.css" media="print" onload="this.media='all'">
|
||||
<link rel="alternate" type="application/rss+xml" title="Chaosmith Systems — Field Notes" href="/blog/feed.xml">
|
||||
<link rel="alternate" type="application/feed+json" title="Chaosmith Systems — Field Notes (JSON)" href="/blog/feed.json">
|
||||
<link rel="stylesheet" href="/src/thirdparty/highlight/default.min.css" media="print" onload="this.media='all'">
|
||||
<script defer src="/src/thirdparty/highlight/highlight.min.js"></script>
|
||||
<script defer src="/src/lang/ergon.js"></script>
|
||||
<script defer src="/src/lang/archasm.js"></script>
|
||||
<style>
|
||||
:root {
|
||||
--bg: #05030a;
|
||||
@@ -137,6 +137,7 @@ const pageTpl = `<!DOCTYPE html>
|
||||
<a href="/blog/">Blog</a>
|
||||
</nav>
|
||||
</div>
|
||||
<a href="/blog/feed.xml" style="margin-left:auto; font-size:13px; text-transform:uppercase; letter-spacing:0.1em; color:var(--text-dim); text-decoration:none;">RSS</a>
|
||||
</header>
|
||||
<main>
|
||||
{{if .Post.HTML}}
|
||||
@@ -166,7 +167,7 @@ const pageTpl = `<!DOCTYPE html>
|
||||
<span>Chaosmith Systems</span>
|
||||
</footer>
|
||||
</div>
|
||||
<script>hljs.highlightAll();</script>
|
||||
<script>window.addEventListener('DOMContentLoaded',()=>hljs.highlightAll());</script>
|
||||
</body>
|
||||
</html>`
|
||||
|
||||
@@ -248,6 +249,30 @@ func RegisterRoutes(mux *http.ServeMux, contentDir string) error {
|
||||
})
|
||||
})
|
||||
|
||||
// RSS feed
|
||||
mux.HandleFunc("/blog/feed.xml", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/rss+xml; charset=utf-8")
|
||||
w.Write([]byte(generateRSS(store)))
|
||||
})
|
||||
|
||||
// JSON feed
|
||||
mux.HandleFunc("/blog/feed.json", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/feed+json; charset=utf-8")
|
||||
w.Write([]byte(generateJSONFeed(store)))
|
||||
})
|
||||
|
||||
// Sitemap
|
||||
mux.HandleFunc("/sitemap.xml", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
|
||||
w.Write([]byte(generateSitemap(store)))
|
||||
})
|
||||
|
||||
// robots.txt
|
||||
mux.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.Write([]byte("User-agent: *\nAllow: /\n\nSitemap: https://chaosmith.systems/sitemap.xml\n"))
|
||||
})
|
||||
|
||||
log.Printf("blog: loaded %d post(s) from %s, mounted at /blog/", store.Count(), contentDir)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user