-
Notifications
You must be signed in to change notification settings - Fork 0
/
riviera.go
204 lines (167 loc) · 4.4 KB
/
riviera.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Riviera is a feed aggregator.
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"sync"
"html/template"
"time"
fsnotify "gopkg.in/fsnotify.v1"
"hawx.me/code/riviera/river"
"hawx.me/code/riviera/river/data"
"hawx.me/code/riviera/river/data/boltdata"
"hawx.me/code/riviera/river/data/memdata"
"hawx.me/code/riviera/river/mapping"
"hawx.me/code/riviera/subscriptions"
"hawx.me/code/riviera/subscriptions/opml"
"hawx.me/code/serve"
)
func printHelp() {
fmt.Println(`Usage: riviera [options] FILE
Riviera is a feed aggregator. It reads a list of feeds in OPML
subscription list format (http://dev.opml.org/spec2.html) given
as FILE, polls these feeds at a customisable interval, and serves
a riverjs (http://riverjs.org) format document at '/river'.
A json list of fetch events is served at '/river/log'
Changes to FILE are watched and will modify the feeds watched, if it
can be successfully parsed.
DISPLAY
--cutoff DUR='-24h'
Time to ignore items after, given in standard go duration format
(see http://golang.org/pkg/time/#ParseDuration).
--refresh DUR='15m'
Time to refresh feeds after. This is the default used, but if
advice is given in the feed itself it may be ignored.
DATA
By default riviera runs with an in memory database.
--boltdb PATH
Use the boltdb file at the given path.
SERVE
--port PORT='8080'
Serve on given port.
--socket SOCK
Serve at given socket, instead.`)
}
var (
cutOff = flag.String("cutoff", "-24h", "")
refresh = flag.String("refresh", "15m", "")
boltdbPath = flag.String("boltdb", "", "")
webPath = flag.String("web", "web", "")
port = flag.String("port", "8080", "")
socket = flag.String("socket", "", "")
)
func loadDatastore() (data.Database, error) {
if *boltdbPath != "" {
return boltdata.Open(*boltdbPath)
}
return memdata.Open(), nil
}
func watchFile(path string, f func()) (io.Closer, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return watcher, err
}
go func() {
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
f()
}
case err := <-watcher.Errors:
if err != nil {
log.Printf("error watching %s: %v", path, err)
}
}
}
}()
return watcher, watcher.Add(path)
}
func parseTemplates(path string) (*template.Template, error) {
return template.New("").Funcs(map[string]interface{}{}).ParseGlob(path + "/template/*.gotmpl")
}
func main() {
flag.Usage = func() { printHelp() }
flag.Parse()
if flag.NArg() == 0 {
printHelp()
return
}
templates, err := parseTemplates(*webPath)
if err != nil {
log.Println(err)
return
}
var wg sync.WaitGroup
waitFor := func(name string, f func() error) {
log.Println(name, "waiting to close")
wg.Add(1)
if err := f(); err != nil {
log.Println("waitFor:", err)
}
wg.Done()
log.Println(name, "closed")
}
opmlPath := flag.Arg(0)
duration, err := time.ParseDuration(*cutOff)
if err != nil {
log.Println(err)
return
}
cacheTimeout, err := time.ParseDuration(*refresh)
if err != nil {
log.Println(err)
return
}
store, err := loadDatastore()
if err != nil {
log.Println(err)
return
}
defer waitFor("datastore", store.Close)
outline, err := opml.Load(opmlPath)
if err != nil {
log.Println(err)
return
}
feeds := river.New(store, river.Options{
Mapping: mapping.DefaultMapping,
CutOff: duration,
Refresh: cacheTimeout,
LogLength: 500,
})
defer waitFor("feeds", feeds.Close)
subs := subscriptions.FromOpml(outline)
for _, sub := range subs.List() {
feeds.Add(sub.URI)
}
watcher, err := watchFile(opmlPath, func() {
log.Printf("reading %s\n", opmlPath)
outline, err := opml.Load(opmlPath)
if err != nil {
log.Printf("could not read %s: %s\n", opmlPath, err)
return
}
added, removed := subscriptions.Diff(subs, subscriptions.FromOpml(outline))
for _, uri := range added {
feeds.Add(uri)
subs.Add(uri)
}
for _, uri := range removed {
feeds.Remove(uri)
subs.Remove(uri)
}
})
if err != nil {
log.Printf("could not start watching %s: %v\n", opmlPath, err)
}
defer waitFor("watcher", watcher.Close)
http.Handle("/", river.List(feeds, templates))
http.Handle("/log", river.Log(feeds, templates))
http.Handle("/public/", http.StripPrefix("/public", http.FileServer(http.Dir(*webPath+"/static"))))
serve.Serve(*port, *socket, http.DefaultServeMux)
wg.Wait()
}