-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss.go
More file actions
62 lines (56 loc) · 1.47 KB
/
rss.go
File metadata and controls
62 lines (56 loc) · 1.47 KB
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
package pubengine
import (
"encoding/xml"
"net/http"
"time"
"github.com/labstack/echo/v4"
)
type rssXML struct {
XMLName xml.Name `xml:"rss"`
Version string `xml:"version,attr"`
Channel rssChannel `xml:"channel"`
}
type rssChannel struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
Items []rssItem `xml:"item"`
}
type rssItem struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
PubDate string `xml:"pubDate"`
GUID string `xml:"guid"`
}
func (a *App) renderRSS(c echo.Context, posts []BlogPost) error {
base := a.Config.URL
items := make([]rssItem, 0, len(posts))
for _, p := range posts {
pubDate := ""
if t, err := time.Parse("2006-01-02", p.Date); err == nil {
pubDate = t.Format(time.RFC1123Z)
}
postURL := BuildURL(base, "blog", p.Slug)
items = append(items, rssItem{
Title: p.Title,
Link: postURL,
Description: p.Summary,
PubDate: pubDate,
GUID: postURL,
})
}
feed := rssXML{
Version: "2.0",
Channel: rssChannel{
Title: a.Config.Name,
Link: base,
Description: a.Config.Description,
Items: items,
},
}
c.Response().Header().Set(echo.HeaderContentType, "application/rss+xml; charset=utf-8")
c.Response().WriteHeader(http.StatusOK)
c.Response().Write([]byte(xml.Header))
return xml.NewEncoder(c.Response()).Encode(feed)
}