aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/server/feed.go52
-rw-r--r--cmd/server/handlers.go6
-rw-r--r--cmd/server/main.go14
3 files changed, 59 insertions, 13 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)