forked from ChimeraCoder/pluto
/
server.go
294 lines (248 loc) · 7.41 KB
/
server.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package main
import (
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"github.com/gorilla/pat"
"github.com/gorilla/sessions"
rss "github.com/jteeuwen/go-pkg-rss"
"html/template"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"log"
"net/http"
"os"
"regexp"
"strconv"
"time"
)
const BCRYPT_COST = 12
const RSS_TIMEOUT = 100
const FEEDS_LIST_FILENAME = "feeds_list.txt"
const BLOGPOSTS_DB = "blogposts"
const POSTS_PER_PAGE = 10
var fetchposts = flag.Bool("fetchposts", false, "fetch blogposts and add them to the database")
var SANITIZE_REGEX = regexp.MustCompile(`<script.*?>.*?<\/script>`)
var AUTHOR_URL_REGEX = regexp.MustCompile(`(.*?)\/rss`)
var (
httpAddr = flag.String("addr", "localhost:8000", "HTTP server address")
baseTmpl string = "templates/base.tmpl"
store = sessions.NewCookieStore([]byte(COOKIE_SECRET))
//The following three variables can be defined using environment variables
//to avoid committing them by mistake
//Alternatively, place variable declarations in a separate conf.go file
//which is already in the .gitignore file
//COOKIE_SECRET = []byte(os.Getenv("COOKIE_SECRET"))
//APP_ID = os.Getenv("APP_ID")
//APP_SECRET = os.Getenv("APP_SECRET")
)
func scrapeRss(uri string, author string) {
feed := rss.New(RSS_TIMEOUT, true, chanHandler, customItemHandler(author))
for {
if err := feed.Fetch(uri, nil); err != nil {
fmt.Fprintf(os.Stderr, "[e.fetch] %s: %s", uri, err)
return
}
log.Printf("Sleeping for %d seconds on %s", feed.SecondsTillUpdate(), uri)
<-time.After(time.Duration(feed.SecondsTillUpdate() * 1e9))
}
}
func chanHandler(feed *rss.Feed, newchannels []*rss.Channel) {
log.Printf("Found %d new channel(s) in %s", len(newchannels), feed.Url)
}
func customItemHandler(author string) func(*rss.Feed, *rss.Channel, []*rss.Item) {
return func(feed *rss.Feed, ch *rss.Channel, newitems []*rss.Item) {
log.Printf("Found %d new item(s) in %s", len(newitems), feed.Url)
for _, item := range newitems {
log.Printf("Item is %+v", item)
//If the author's name isn't defined, we should add it
if item.Author.Name == "" {
item.Author.Name = author
item.Author.Uri = AUTHOR_URL_REGEX.FindStringSubmatch(feed.Url)[1]
}
item_updated, err := NewItem(*item)
if err != nil {
panic(err)
}
savePost(item_updated)
}
}
}
//Given an RSS item, save it in mongodb
func savePost(post Item) error {
return withCollection(BLOGPOSTS_DB, func(c *mgo.Collection) error {
return c.Insert(post)
})
}
func servePosts(w http.ResponseWriter, r *http.Request) {
var err error
//Default to providing the first page
page := 1
page_s := r.FormValue("page")
if page_s != "" {
//No page specified
page, err = strconv.Atoi(page_s)
if err != nil {
panic(err)
}
}
//TODO make this a proper query for the feeds we want
var posts []Item
if err := withCollection(BLOGPOSTS_DB, func(c *mgo.Collection) error {
return c.Find(bson.M{}).Skip(POSTS_PER_PAGE * (page - 1)).Limit(POSTS_PER_PAGE).Sort("-pubdateparsed").All(&posts)
}); err != nil {
panic(err)
}
log.Printf("Fetching %d posts", len(posts))
renderHtml := func(raw_html string) template.HTML {
return template.HTML(raw_html)
}
funcs := template.FuncMap{
"RenderHtml": renderHtml,
}
authors, err := allAuthors()
if err != nil {
panic(err)
}
s1 := template.New("base").Funcs(funcs)
s1, err = s1.ParseFiles("templates/base.tmpl", "templates/posts.tmpl")
//s1, err := template.ParseFiles("templates/base.tmpl", "templates/posts.tmpl")
if err != nil {
panic(err)
}
s1 = s1.Funcs(funcs)
s1.ExecuteTemplate(w, "base", struct {
Posts []Item
Authors []rss.Author
}{posts, authors})
}
//Return a the feeds ([]rss.Item) serialized in JSON
func serveFeeds(w http.ResponseWriter, r *http.Request) {
var err error
//Default to providing the first page
page := 1
page_s := r.FormValue("page")
if page_s != "" {
//No page specified
page, err = strconv.Atoi(page_s)
if err != nil {
panic(err)
}
}
log.Printf("Page %s", page_s)
log.Print(page)
//TODO make this a proper query for the feeds we want
var posts []Item
if err := withCollection(BLOGPOSTS_DB, func(c *mgo.Collection) error {
return c.Find(bson.M{}).Skip(POSTS_PER_PAGE * (page - 1)).Limit(POSTS_PER_PAGE).All(&posts)
}); err != nil {
panic(err)
}
items_sanitized := make([]Item, len(posts))
for i, item := range posts {
items_sanitized[i] = sanitizeItem(item)
}
bts, err := json.Marshal(posts)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(bts))
return
}
func allAuthors() (authors []rss.Author, err error) {
err = withCollection(BLOGPOSTS_DB, func(c *mgo.Collection) error {
return c.Find(bson.M{}).Distinct("author", &authors)
})
return
}
func serveAuthorInfo(w http.ResponseWriter, r *http.Request) {
authors, err := allAuthors()
if err != nil {
panic(err)
}
bts, err := json.Marshal(authors)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(bts))
return
}
//sanitizeItem sanitizes the HTML content by removing Javascript, etc.
//TODO make this not a terrible hack
func sanitizeItem(item Item) Item {
//This is not currently safe to use for untrusted input, as it can be exploited trivially
//However, it requires thought to exploit it, so it should prevent _accidental_ javascript spillage
//It cannot remove javascript embedded in tag attributes (such as 'onclick:', etc.)
item.Description = SANITIZE_REGEX.ReplaceAllString(item.Description, "")
return item
}
func parseFeeds(filename string) ([][]string, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
reader := csv.NewReader(file)
reader.Comma = '\t'
rows, err := reader.ReadAll()
if err != nil {
return nil, err
}
return rows, err
}
func main() {
flag.Parse()
var err error
log.Print("Dialing mongodb database")
mongodb_session, err = mgo.Dial(MONGODB_URL)
if err != nil {
panic(err)
}
log.Print("Succesfully dialed mongodb database")
err = mongodb_session.DB(MONGODB_DATABASE).Login(MONGODB_USERNAME, MONGODB_PASSWORD)
r := pat.New()
//Create a unique index on 'guid', so that entires will not be duplicated
//Any duplicate entries will be dropped silently when insertion is attempted
guid_index := mgo.Index{
Key: []string{"guid"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
mongodb_session.DB(MONGODB_DATABASE).C(BLOGPOSTS_DB).EnsureIndex(guid_index)
if *fetchposts {
feeds, err := parseFeeds(FEEDS_LIST_FILENAME)
if err != nil {
panic(err)
}
for _, feed_info := range feeds {
if len(feed_info) != 2 {
panic(fmt.Errorf("Expect csv with 2 elements per row; received row with %d elements", len(feed_info)))
}
feed_url := feed_info[0]
feed_author := feed_info[1]
log.Printf("Found %s", feed_url)
go func(uri string, author string) {
scrapeRss(uri, author)
}(feed_url, feed_author)
}
} else {
log.Print("Skipping fetching posts - blog posts will NOT be updated")
}
//Order of routes matters
//Routes *will* match prefixes
http.Handle("/static/", http.FileServer(http.Dir("public")))
r.Get("/feeds/all", serveFeeds)
r.Get("/authors/all", serveAuthorInfo)
r.Get("/", servePosts)
//r.Get("/", serveHome)
http.Handle("/", r)
log.Printf("Starting HTTP server listening on %v", *httpAddr)
if err := http.ListenAndServe(*httpAddr, nil); err != nil {
log.Fatalf("Error listening, %v", err)
}
}