-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
284 lines (252 loc) · 6.81 KB
/
app.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
package main
import (
"embed"
"fmt"
"html/template"
"log"
"net/http"
"os"
"path"
"strconv"
"strings"
"sync"
"time"
"github.com/gorilla/feeds"
"github.com/mmcdole/gofeed"
)
const EPRINT_FEED_URL = "https://eprint.iacr.org/rss/atom.xml"
//go:embed templates/*
var resources embed.FS
var t = template.Must(template.ParseFS(resources, "templates/*"))
type Feed struct {
mu sync.Mutex
feed *feeds.Feed
}
// feed of recent eprints
var eprintFeed Feed
// feed of recent eprints organized by year-then-week
var eprintYearWeekFeed = make(map[int]map[int]*Feed)
func getFeed() (*feeds.Feed, error) {
fp := gofeed.NewParser()
feed, err := fp.ParseURL(EPRINT_FEED_URL)
if err != nil {
return nil, err
}
return gofeedToGorillaFeed(feed)
}
func updateFeed() {
eprintFeed.mu.Lock()
defer eprintFeed.mu.Unlock()
feed, err := getFeed()
if err != nil {
log.Printf("failed to parse rss at %s, encountered error: %s\n", time.Now().Format(time.RFC1123Z), err)
}
eprintFeed.feed = feed
// update the weekly feed
for _, item := range feed.Items {
year, week := item.Created.ISOWeek()
// create a feed for the year, if it doesn't already exist
_, ok := eprintYearWeekFeed[year]
if !ok {
eprintYearWeekFeed[year] = make(map[int]*Feed)
}
// append items to the week feed
if eprintYearWeekFeed[year][week] == nil {
eprintYearWeekFeed[year][week] = &Feed{feed: &feeds.Feed{}}
}
weekFeed := eprintYearWeekFeed[year][week]
weekFeed.mu.Lock()
included := false
for _, i := range weekFeed.feed.Items {
if i.Link.Href == item.Link.Href {
included = true
}
}
if !included {
weekFeed.feed.Items = append(weekFeed.feed.Items, item)
}
weekFeed.mu.Unlock()
}
}
func updateFeedEveryTwoHours() {
c := time.Tick(2 * time.Hour)
for range c {
updateFeed()
}
}
func serverError(w http.ResponseWriter, r *http.Request) {
http.Error(w, "failed to retrieve page, please try again later.", http.StatusInternalServerError)
}
func weekHandler(w http.ResponseWriter, r *http.Request) {
url := path.Clean(r.URL.Path)
matched, err := path.Match("/week/[0-9]*/[0-9]*", url)
if err != nil {
serverError(w, r)
return
}
if !matched {
http.NotFound(w, r)
return
}
url = url[len("/week"):]
yearStr, weekStr := path.Split(url)
if matched, err = path.Match("/[0-9]*/", yearStr); !matched || (err != nil) {
serverError(w, r)
return
}
// strip leading and trailing slashes
yearStr = yearStr[1:]
yearStr = yearStr[:len(yearStr)-1]
year, err1 := strconv.Atoi(yearStr)
week, err2 := strconv.Atoi(weekStr)
if err1 != nil || err2 != nil {
serverError(w, r)
return
}
yearFeed, ok := eprintYearWeekFeed[year]
if !ok || yearFeed == nil {
http.NotFound(w, r)
return
}
weekFeed, ok := yearFeed[week]
if !ok || weekFeed == nil {
http.NotFound(w, r)
return
}
// find the start of the week by seeking from Dec 29 of the previous year.
weekStart := time.Date(year-1, time.December, 29, 0, 0, 0, 0, time.UTC)
for {
y, w := weekStart.ISOWeek()
if y == year && w == week {
break
}
weekStart = weekStart.Add(24 * time.Hour)
}
weekEnd := weekStart.Add(7 * 24 * time.Hour)
hoursToWeekEnd := 0
if weekEnd.After(time.Now()) {
hoursToWeekEnd = int(time.Until(weekEnd).Hours())
}
data := map[string]any{
"year": year,
"week": week,
"weekStart": weekStart.Format(time.RFC1123Z),
"weekEnd": weekEnd.Format(time.RFC1123Z),
"hoursToWeekEnd": hoursToWeekEnd,
"number": len(weekFeed.feed.Items),
"listings": weekFeed.feed.Items,
"LastUpdated": eprintFeed.feed.Updated.UTC().Format(time.RFC1123Z),
"ElapsedSinceLastUpdated": strconv.Itoa(int(time.Now().UTC().Sub(eprintFeed.feed.Updated.UTC()).Minutes())),
}
err = t.ExecuteTemplate(w, "week-template.html", data)
if err != nil {
serverError(w, r)
}
}
func feedHandler(w http.ResponseWriter, r *http.Request) {
eprintFeed.mu.Lock()
defer eprintFeed.mu.Unlock()
query := r.URL.Query()
keywords := query["keyword"]
showAllItems := (query.Get("show_all_items") == "true")
title := "custom eprint feed with keywords: \"" + strings.Join(keywords, "\", \"") + "\""
if showAllItems {
title = "full eprint feed"
}
customfeed := &feeds.Feed{
Title: title,
Link: &feeds.Link{Href: "https://eprint.fans" + r.URL.String()},
Description: "generated using eprint.fans from https://eprint.iacr.org/rss/atom.xml",
Updated: eprintFeed.feed.Updated,
}
customfeed.Items = []*feeds.Item{}
for _, item := range eprintFeed.feed.Items {
if showAllItems {
customfeed.Items = append(
customfeed.Items,
&feeds.Item{
Title: item.Title,
Link: item.Link,
Author: item.Author,
Description: item.Description,
Id: item.Id,
Updated: item.Updated,
Created: item.Created,
},
)
} else {
relevant := false
triggeringKeyword := ""
for _, keyword := range keywords {
if strings.Contains(
strings.ToLower(item.Title),
strings.ToLower(keyword),
) ||
strings.Contains(
strings.ToLower(item.Description),
strings.ToLower(keyword),
) ||
strings.Contains(
strings.ToLower(item.Author.Name),
strings.ToLower(keyword),
) {
relevant = true
triggeringKeyword = keyword
}
}
if !relevant {
continue
}
customfeed.Items = append(
customfeed.Items,
&feeds.Item{
Title: item.Title,
Link: item.Link,
Author: item.Author,
Description: fmt.Sprintf("[[Triggering Keyword: \"%s\"]] ", triggeringKeyword) + item.Description,
Id: item.Id,
Updated: item.Updated,
Created: item.Created,
},
)
}
}
atom, err := customfeed.ToAtom()
if err != nil {
fmt.Fprintf(w, "failed to generate feed with error: %s", err)
return
}
fmt.Fprint(w, atom)
}
func styleHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/css")
err := t.ExecuteTemplate(w, "style.css", nil)
if err != nil {
serverError(w, r)
}
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]string{
"LastUpdated": eprintFeed.feed.Updated.UTC().Format(time.RFC1123Z),
"ElapsedSinceLastUpdated": strconv.Itoa(int(time.Now().UTC().Sub(eprintFeed.feed.Updated.UTC()).Minutes())),
}
err := t.ExecuteTemplate(w, "index-template.html", data)
if err != nil {
serverError(w, r)
}
}
func main() {
updateFeed()
// update feed again every 2 hours.
go updateFeedEveryTwoHours()
http.HandleFunc("/", rootHandler)
http.HandleFunc("/feed/", feedHandler)
http.HandleFunc("/week/", weekHandler)
http.HandleFunc("/style.css", styleHandler)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Println("listening on", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}