forked from jlelse/GoBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feeds.go
87 lines (82 loc) · 2.29 KB
/
feeds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"io"
"net/http"
"strings"
"time"
"github.com/araddon/dateparse"
"github.com/jlelse/feeds"
"go.goblog.app/app/pkgs/bufferpool"
"go.goblog.app/app/pkgs/contenttype"
)
type feedType string
const (
noFeed feedType = ""
rssFeed feedType = "rss"
atomFeed feedType = "atom"
jsonFeed feedType = "json"
minRssFeed feedType = "min.rss"
minAtomFeed feedType = "min.atom"
minJsonFeed feedType = "min.json"
)
func (a *goBlog) generateFeed(blog string, f feedType, w http.ResponseWriter, r *http.Request, posts []*post, title, description string) {
now := time.Now()
title = a.renderMdTitle(defaultIfEmpty(title, a.cfg.Blogs[blog].Title))
description = defaultIfEmpty(description, a.cfg.Blogs[blog].Description)
feed := &feeds.Feed{
Title: title,
Description: description,
Link: &feeds.Link{Href: a.getFullAddress(strings.TrimSuffix(r.URL.Path, "."+string(f)))},
Created: now,
Author: &feeds.Author{
Name: a.cfg.User.Name,
Email: a.cfg.User.Email,
},
Image: &feeds.Image{
Url: a.cfg.User.Picture,
},
}
for _, p := range posts {
buf := bufferpool.Get()
switch f {
case minRssFeed, minAtomFeed, minJsonFeed:
a.minFeedHtml(buf, p)
default:
a.feedHtml(buf, p)
}
feed.Add(&feeds.Item{
Title: p.RenderedTitle,
Link: &feeds.Link{Href: a.fullPostURL(p)},
Description: a.postSummary(p),
Id: p.Path,
Content: buf.String(),
Created: timeNoErr(dateparse.ParseLocal(p.Published)),
Updated: timeNoErr(dateparse.ParseLocal(p.Updated)),
})
bufferpool.Put(buf)
}
var feedWriteFunc func(w io.Writer) error
var feedMediaType string
switch f {
case rssFeed, minRssFeed:
feedMediaType = contenttype.RSS
feedWriteFunc = feed.WriteRss
case atomFeed, minAtomFeed:
feedMediaType = contenttype.ATOM
feedWriteFunc = feed.WriteAtom
case jsonFeed, minJsonFeed:
feedMediaType = contenttype.JSONFeed
feedWriteFunc = feed.WriteJSON
default:
a.serve404(w, r)
return
}
pipeReader, pipeWriter := io.Pipe()
go func() {
writeErr := feedWriteFunc(pipeWriter)
_ = pipeWriter.CloseWithError(writeErr)
}()
w.Header().Set(contentType, feedMediaType+contenttype.CharsetUtf8Suffix)
minifyErr := a.min.Get().Minify(feedMediaType, w, pipeReader)
_ = pipeReader.CloseWithError(minifyErr)
}