Simple blogging system written in Go https://goblog.app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

77 lines
1.9 KiB

package main
import (
"net/http"
"strings"
"time"
"github.com/araddon/dateparse"
"github.com/jlelse/feeds"
"go.goblog.app/app/pkgs/contenttype"
)
type feedType string
const (
noFeed feedType = ""
rssFeed feedType = "rss"
atomFeed feedType = "atom"
jsonFeed feedType = "json"
)
func (a *goBlog) generateFeed(blog string, f feedType, w http.ResponseWriter, r *http.Request, posts []*post, title string, description string) {
now := time.Now()
if title == "" {
title = a.cfg.Blogs[blog].Title
}
title = a.renderMdTitle(title)
if description == "" {
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 {
feed.Add(&feeds.Item{
Title: p.RenderedTitle,
Link: &feeds.Link{Href: a.fullPostURL(p)},
Description: a.postSummary(p),
Id: p.Path,
Content: string(a.postHtml(p, true)),
Created: timeNoErr(dateparse.ParseLocal(p.Published)),
Updated: timeNoErr(dateparse.ParseLocal(p.Updated)),
})
}
var err error
var feedString, feedMediaType string
switch f {
case rssFeed:
feedMediaType = contenttype.RSS
feedString, err = feed.ToRss()
case atomFeed:
feedMediaType = contenttype.ATOM
feedString, err = feed.ToAtom()
case jsonFeed:
feedMediaType = contenttype.JSONFeed
feedString, err = feed.ToJSON()
default:
return
}
if err != nil {
w.Header().Del(contentType)
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set(contentType, feedMediaType+contenttype.CharsetUtf8Suffix)
_, _ = a.min.Write(w, feedMediaType, []byte(feedString))
}