aboutsummaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
authorChristine Dodrill <me@christine.website>2017-03-29 00:26:50 -0700
committerChristine Dodrill <me@christine.website>2017-03-29 00:26:50 -0700
commitb89387f6bbb010907dfa85ee0c0bab0cf8b34dfb (patch)
tree91deef87b78892c30839a99088362b70bf78eee4 /backend
parenta59e48d9485f6c80f397df3bbcbe9b09ac54a435 (diff)
downloadxesite-b89387f6bbb010907dfa85ee0c0bab0cf8b34dfb.tar.xz
xesite-b89387f6bbb010907dfa85ee0c0bab0cf8b34dfb.zip
support RSS feed generation
Diffstat (limited to 'backend')
-rw-r--r--backend/christine.website/hash.go14
-rw-r--r--backend/christine.website/main.go63
2 files changed, 77 insertions, 0 deletions
diff --git a/backend/christine.website/hash.go b/backend/christine.website/hash.go
new file mode 100644
index 0000000..ed6112c
--- /dev/null
+++ b/backend/christine.website/hash.go
@@ -0,0 +1,14 @@
+package main
+
+import (
+ "crypto/md5"
+ "fmt"
+)
+
+// Hash is a simple wrapper around the MD5 algorithm implementation in the
+// Go standard library. It takes in data and a salt and returns the hashed
+// representation.
+func Hash(data string, salt string) string {
+ output := md5.Sum([]byte(data + salt))
+ return fmt.Sprintf("%x", output)
+}
diff --git a/backend/christine.website/main.go b/backend/christine.website/main.go
index ce0e507..0f43487 100644
--- a/backend/christine.website/main.go
+++ b/backend/christine.website/main.go
@@ -13,7 +13,9 @@ import (
"time"
"github.com/Xe/asarfs"
+ "github.com/Xe/ln"
"github.com/gernest/front"
+ "github.com/gorilla/feeds"
"github.com/urfave/negroni"
)
@@ -155,12 +157,73 @@ func main() {
port = "9090"
}
+ http.HandleFunc("/blog.rss", createFeed)
+ http.HandleFunc("/blog.atom", createAtom)
+
n := negroni.Classic()
n.UseHandler(http.DefaultServeMux)
log.Fatal(http.ListenAndServe(":"+port, n))
}
+var bootTime = time.Now()
+
+var feed = &feeds.Feed{
+ Title: "Christine Dodrill's Blog",
+ Link: &feeds.Link{Href: "https://christine.website/blog"},
+ Description: "My blog posts and rants about various technology things.",
+ Author: &feeds.Author{Name: "Christine Dodrill", Email: "me@christine.website"},
+ Created: bootTime,
+ Copyright: "This work is copyright Christine Dodrill. My viewpoints are my own and not the view of any employer past, current or future.",
+}
+
+func init() {
+ for _, item := range posts {
+ itime, _ := time.Parse("2006-01-02", item.Date)
+ feed.Items = append(feed.Items, &feeds.Item{
+ Title: item.Title,
+ Link: &feeds.Link{Href: "https://christine.website/" + item.Link},
+ Description: item.Summary,
+ Created: itime,
+ })
+ }
+}
+
+// IncrediblySecureSalt *******
+const IncrediblySecureSalt = "hunter2"
+
+func createFeed(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/rss+xml")
+ w.Header().Set("ETag", Hash(bootTime.String(), IncrediblySecureSalt))
+
+ err := feed.WriteRss(w)
+ if err != nil {
+ http.Error(w, "Internal server error", http.StatusInternalServerError)
+ ln.Error(err, ln.F{
+ "remote_addr": r.RemoteAddr,
+ "action": "generating_rss",
+ "uri": r.RequestURI,
+ "host": r.Host,
+ })
+ }
+}
+
+func createAtom(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/atom+xml")
+ w.Header().Set("ETag", Hash(bootTime.String(), IncrediblySecureSalt))
+
+ err := feed.WriteAtom(w)
+ if err != nil {
+ http.Error(w, "Internal server error", http.StatusInternalServerError)
+ ln.Error(err, ln.F{
+ "remote_addr": r.RemoteAddr,
+ "action": "generating_rss",
+ "uri": r.RequestURI,
+ "host": r.Host,
+ })
+ }
+}
+
func writeBlogPosts(w http.ResponseWriter, r *http.Request) {
p := []interface{}{}
for _, post := range posts {