aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitchell Riedstra <mitch@riedstra.dev>2021-07-12 23:06:42 -0400
committerMitchell Riedstra <mitch@riedstra.dev>2021-07-12 23:06:42 -0400
commit2734c671324e980ef7424e367461ebaa4e0034a5 (patch)
treed4ee457c18002011b9e5a5927baa34e5b6d7b16c
parent3f47f136de6a79adedbe16329586944c6a90ee65 (diff)
downloadgo-website-2734c671324e980ef7424e367461ebaa4e0034a5.tar.gz
go-website-2734c671324e980ef7424e367461ebaa4e0034a5.tar.xz
Add atom feed to site, require config.yml
-rw-r--r--cmd/server/feed.go52
-rw-r--r--cmd/server/handlers.go6
-rw-r--r--cmd/server/main.go14
-rw-r--r--page/index.go3
-rw-r--r--page/page.go9
-rw-r--r--page/pagelist.go16
-rw-r--r--page/render.go2
7 files changed, 77 insertions, 25 deletions
diff --git a/cmd/server/feed.go b/cmd/server/feed.go
index 278c706..2d4a75b 100644
--- a/cmd/server/feed.go
+++ b/cmd/server/feed.go
@@ -7,6 +7,7 @@ import (
"fmt"
"log"
"net/http"
+ "strconv"
"strings"
"time"
@@ -30,7 +31,7 @@ type Link struct {
type Content struct {
Type string `xml:"type,attr"`
- Data []byte `xml:"chardata"`
+ Data string `xml:",chardata"`
}
type Entry struct {
@@ -107,8 +108,31 @@ func (a Atom) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(a2, start)
}
+// FeedHandler takes care of pulling from the index all of the relevant posts
+// and dumping them into an Atom feed.
+//
+// Relevant query parameters are:
+//
+// "content" if unset, or set to false content is omitted from the feed
+// "limit=n" stop at "n" and return the feed
+//
func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
+ var addContent bool
+ var limit int
+
+ if _, ok := r.URL.Query()["content"]; ok {
+ if r.URL.Query().Get("content") != "false" {
+ addContent = true
+ }
+ }
+
+ if l := r.URL.Query().Get("limit"); l != "" {
+ i, err := strconv.Atoi(l)
+ if err == nil {
+ limit = i
+ }
+ }
tag, ok := vars["tag"]
if !ok {
@@ -130,6 +154,12 @@ func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) {
return
}
+ pages, dateless := pages.RemoveDateless()
+ for _, p := range dateless {
+ log.Printf("Warning, page %s has no Date field. Skipping inclusion on feed", p)
+ }
+ pages.SortDate()
+
feed := &Atom{
Author: a.Author,
Title: a.Title,
@@ -140,11 +170,9 @@ func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) {
entries := []Entry{}
- for _, p := range pages {
-
- if p.Date == nil {
- log.Printf("Warning, page %s has no Date field. Skipping inclusion on feed", p)
- continue
+ for n, p := range pages {
+ if limit != 0 && n >= limit {
+ break
}
content := &bytes.Buffer{}
@@ -155,12 +183,18 @@ func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) {
return
}
- entries = append(entries, Entry{
+ entry := Entry{
Title: p.Title,
Updated: &p.Date.Time,
Links: []Link{Link{Href: strings.Join([]string{a.SiteURL, p.Path()}, "/")}},
- // Content: Content{Type: "html", Data: content.Bytes()},
- })
+ }
+
+ if addContent {
+ entry.Content = &Content{Type: "html", Data: content.String()}
+ }
+
+ entries = append(entries, entry)
+
}
feed.Entries = entries
diff --git a/cmd/server/handlers.go b/cmd/server/handlers.go
index a54e5ee..5ea89cd 100644
--- a/cmd/server/handlers.go
+++ b/cmd/server/handlers.go
@@ -1,6 +1,7 @@
package main
import (
+ "fmt"
"net/http"
"path/filepath"
@@ -8,6 +9,8 @@ import (
"riedstra.dev/mitch/go-website/page"
)
+var FeedPrefixDefault = ".feeds"
+
type App struct {
ReIndexPath string
StaticDirectory string
@@ -19,6 +22,7 @@ type App struct {
SiteURL string
FeedId string
Updated page.PageTime
+ FeedPrefix string
}
func (a *App) PageHandler(w http.ResponseWriter, r *http.Request) {
@@ -56,7 +60,7 @@ func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rtr := mux.NewRouter()
rtr.HandleFunc(a.ReIndexPath, a.RebuildIndexHandler)
rtr.PathPrefix("/static/").Handler(a.StaticHandler())
- rtr.PathPrefix("/.feeds/{tag}").HandlerFunc(a.FeedHandler)
+ rtr.PathPrefix(fmt.Sprintf("/%s/{tag}", a.FeedPrefix)).HandlerFunc(a.FeedHandler)
rtr.PathPrefix("/").HandlerFunc(a.PageHandler)
rtr.ServeHTTP(w, r)
}
diff --git a/cmd/server/main.go b/cmd/server/main.go
index b2a95c3..b1e53d9 100644
--- a/cmd/server/main.go
+++ b/cmd/server/main.go
@@ -37,7 +37,8 @@ func main() {
directory := fl.String("d", ".", "Directory to serve.")
version := fl.Bool("v", false, "Print the version then exit")
fl.StringVar(&page.TimeFormat, "T", page.TimeFormat, "Print the version then exit")
- indexPath := fl.String("i", "/reIndex",
+ defaultIndexPath := "/reIndex"
+ indexPath := fl.String("i", defaultIndexPath,
"Path in which, when called will rebuild the index and clear the cache")
_ = fl.Parse(os.Args[1:])
@@ -55,8 +56,15 @@ func main() {
app = &App{}
}
- app.ReIndexPath = *indexPath
- app.StaticDirectory = "static"
+ if app.ReIndexPath == "" || *indexPath != defaultIndexPath {
+ app.ReIndexPath = *indexPath
+ }
+ if app.StaticDirectory == "" {
+ app.StaticDirectory = "static"
+ }
+ if app.FeedPrefix == "" {
+ app.FeedPrefix = FeedPrefixDefault
+ }
b, _ := yaml.Marshal(app)
os.Stderr.Write(b)
diff --git a/page/index.go b/page/index.go
index a453335..f9f2df5 100644
--- a/page/index.go
+++ b/page/index.go
@@ -1,7 +1,6 @@
package page
import (
- "fmt"
"os"
"path/filepath"
"strings"
@@ -49,7 +48,7 @@ func (p *Page) Index() (map[string]PageList, error) {
err = p2.Read()
if err != nil {
- fmt.Fprintln(os.Stderr, "Error encountered: ", err)
+ Logger.Println("Error encountered: ", err)
return err
}
diff --git a/page/page.go b/page/page.go
index ecd2268..8b84099 100644
--- a/page/page.go
+++ b/page/page.go
@@ -61,10 +61,6 @@ type Page struct {
// .Global care must be taken when utilizing this functionality
var Global interface{}
-// CachePages determines whether or not the rendered page will be stored in
-// memory
-var CachePages = true
-
// CacheIndex determines whether or not the index will be cached in memory
// or rebuilt on each call
var CacheIndex = true
@@ -105,11 +101,6 @@ func (p *Page) Global() interface{} {
return Global
}
-// SetVars Will set to `nil` if provided
-func (p *Page) SetVars(vars map[string]interface{}) {
- p.Vars = vars
-}
-
// Renders a page
func (p *Page) Render(wr io.Writer) error {
if err := p.Read(); err != nil {
diff --git a/page/pagelist.go b/page/pagelist.go
index a5cf844..f2140f9 100644
--- a/page/pagelist.go
+++ b/page/pagelist.go
@@ -8,6 +8,22 @@ import (
// by the date, or date reversed
type PageList []*Page
+// RemoveDateless returns two PageLists, the first with valid dates,
+// and the second without. This is useful if you need a PageList which
+// will run SortDate and SortDateReverse without issue
+func (p PageList) RemoveDateless() (PageList, PageList) {
+ with := PageList{}
+ without := PageList{}
+ for _, p := range p {
+ if p.Date != nil {
+ with = append(with, p)
+ } else {
+ without = append(without, p)
+ }
+ }
+ return with, without
+}
+
func (p PageList) SortDate() PageList {
sort.Slice(p, func(i, j int) bool {
return p[i].Time().After(p[j].Time())
diff --git a/page/render.go b/page/render.go
index 4378675..07b1b88 100644
--- a/page/render.go
+++ b/page/render.go
@@ -21,7 +21,7 @@ func Render(w http.ResponseWriter, r *http.Request,
p := NewPage(path)
if vars != nil {
- p.SetVars(vars)
+ p.Vars = vars
}
err := p.Render(w)