~jheckt/PhotographyWebsite

a2433321e5757c437caa6c887ca2cfd5bfe5a7d9 — jheckt 1 year, 10 months ago 268f048 + a32aebf
Merge branch 'main' of git.sr.ht:~jheckt/PhotographyWebsite
5 files changed, 78 insertions(+), 1 deletions(-)

M go.mod
M go.sum
M main.go
M routing.go
A rss.go
M go.mod => go.mod +4 -1
@@ 7,4 7,7 @@ require (
	github.com/yuin/goldmark v1.4.13
)

require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
require (
	github.com/gorilla/feeds v1.1.1 // indirect
	golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)

M go.sum => go.sum +2 -0
@@ 1,5 1,7 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/feeds v1.1.1 h1:HwKXxqzcRNg9to+BbvJog4+f3s/xzvtZXICcQGutYfY=
github.com/gorilla/feeds v1.1.1/go.mod h1:Nk0jZrvPFZX1OBe5NPiddPw7CfwF6Q9eqzaBbaightA=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=

M main.go => main.go +3 -0
@@ 33,6 33,9 @@ func main() {
	// Serve the blog
	http.HandleFunc("/blog/", serveBlog)

	// Serve the blog rss
	http.HandleFunc("/rss/", serveRSS)

	// Serve static files from the static directory
	http.HandleFunc("/css/main.css", handleCSS)


M routing.go => routing.go +8 -0
@@ 205,3 205,11 @@ func splitRequestURI(uri string) PathInfo {

	return PathInfo{PathVariables: params, Length: plength}
}

// Serves a blog post given a filename
func serveRSS(w http.ResponseWriter, r *http.Request) {
	rss := rss()
	w.Header().Set("Content-Type", "application/xml")
	w.Write([]byte(rss))

}

A rss.go => rss.go +61 -0
@@ 0,0 1,61 @@
// Makes an RSS feed to seve at /blog-rss.xml

package main

import (
	"strings"
	"time"
	"unicode"

	"github.com/gorilla/feeds"

	log "github.com/sirupsen/logrus"
)

func rss() string {
	log.Debug("Starting RSS feed generation")
	feed := feeds.Feed{
		Title:       "Jeremy Heckt's Photography Blog",
		Link:        &feeds.Link{Href: "https://photography.jeremyheckt.com/blog/"},
		Description: "Let's talk about photos and stuff",
		Author: &feeds.Author{Name: "Jeremy Heckt",
			Email: "jeremy@jeremyheckt.com"},
	}

	feed.Items = make([]*feeds.Item, 0)

	blog := loadBlogPosts()

	for _, filename := range blog.SortedPosts {

		sdate := blog.PostDateMap[filename]

		sdate = strings.Map(func(r rune) rune {
			if unicode.IsPrint(r) {
				return r
			}
			return -1
		}, sdate)

		pdate, error := time.Parse("2006-01-02",
			strings.Replace(sdate, " ", "", -1))

		if error != nil {
			log.Error(error)
		}

		item := feeds.Item{
			Title:   blog.PostFiles[filename],
			Link:    &feeds.Link{Href: "https://photography.jeremyheckt.com/blog/" + filename},
			Created: pdate,
		}
		feed.Items = append(feed.Items, &item)
	}

	rss, err := feed.ToRss()
	if err != nil {
		log.Error("Error processing RSS feed: ", err)
	}

	return rss
}