From 60226001102de6ca0475cb9852a99f9e1b722937 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Wed, 29 Mar 2017 09:48:56 -0700 Subject: [PATCH] rss feed generation in its own file --- backend/christine.website/main.go | 60 --------------------------- backend/christine.website/rss.go | 67 +++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 60 deletions(-) create mode 100644 backend/christine.website/rss.go diff --git a/backend/christine.website/main.go b/backend/christine.website/main.go index d37d185..fa3e0ab 100644 --- a/backend/christine.website/main.go +++ b/backend/christine.website/main.go @@ -13,9 +13,7 @@ import ( "time" "github.com/Xe/asarfs" - "github.com/Xe/ln" "github.com/gernest/front" - "github.com/gorilla/feeds" "github.com/urfave/negroni" ) @@ -169,64 +167,6 @@ func main() { 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 { diff --git a/backend/christine.website/rss.go b/backend/christine.website/rss.go new file mode 100644 index 0000000..b0cd445 --- /dev/null +++ b/backend/christine.website/rss.go @@ -0,0 +1,67 @@ +package main + +import ( + "net/http" + "time" + + "github.com/Xe/ln" + "github.com/gorilla/feeds" +) + +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, + }) + } +}