-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
509 lines (467 loc) · 11.4 KB
/
main.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
package main
import (
"context"
"database/sql"
"embed"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"text/template"
"time"
"github.com/hashicorp/go-multierror"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
_ "github.com/mattn/go-sqlite3"
"github.com/mmcdole/gofeed"
"golang.org/x/exp/slices"
)
var (
basePath string
db *sql.DB
//go:embed templates/*
tmplFS embed.FS
//go:embed static/*
staticFS embed.FS
)
type Template struct {
templates *template.Template
}
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func initDB(db *sql.DB) error {
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS feeds (
id INTEGER PRIMARY KEY AUTOINCREMENT,
url TEXT NOT NULL,
title TEXT NOT NULL,
fetched_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS articles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
feed_id INTEGER NOT NULL,
guid TEXT NOT NULL,
title TEXT NOT NULL,
url TEXT NOT NULL,
is_read INTEGER NOT NULL
);
`)
return err
}
func getIndex(c echo.Context) error {
// Redirect to /feeds/unread
return c.Redirect(http.StatusFound, basePath+"/feeds/unread")
}
func getSettings(c echo.Context) error {
feedURLs := []string{}
rows, err := db.Query(`SELECT url FROM feeds`)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
defer rows.Close()
for rows.Next() {
var url string
err := rows.Scan(&url)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
feedURLs = append(feedURLs, url)
}
// Sort feedURLs in ascending order.
slices.Sort(feedURLs)
return c.Render(http.StatusOK, "settings.html", struct {
BasePath string
URLs string
}{
BasePath: basePath,
URLs: strings.Join(feedURLs, "\r\n"),
})
}
func postSettings(c echo.Context) error {
// Get "urls" from form parameters.
rawUrls := strings.Split(c.FormValue("urls"), "\r\n")
urls := make([]string, 0, len(rawUrls))
for _, rawUrl := range rawUrls {
url := strings.TrimSpace(rawUrl)
if url != "" {
urls = append(urls, url)
}
}
existingURLs := make(map[string]bool)
rows, err := db.Query(`SELECT url FROM feeds`)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
defer rows.Close()
for rows.Next() {
var url string
err := rows.Scan(&url)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
existingURLs[url] = true
}
for _, url := range urls {
if existingURLs[url] {
continue
}
_, err := db.Exec(
`INSERT INTO feeds (url, title, fetched_at) VALUES (?, ?, ?)`,
url,
"",
time.Now().AddDate(0, 0, -1).UTC().Format(time.RFC3339),
)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
}
// Remove:
for existingURL := range existingURLs {
// If existingURL is not in urls, it will be removed.
found := false
for _, url := range urls {
if existingURL == url {
found = true
break
}
}
if found {
continue
}
// Remove feed and articles.
_, err = db.Exec(`DELETE FROM articles WHERE feed_id = (SELECT id FROM feeds WHERE url = ?)`, existingURL)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
_, err := db.Exec(`DELETE FROM feeds WHERE url = ?`, existingURL)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
}
return c.Redirect(http.StatusSeeOther, basePath+"/settings")
}
func fetchOneFeed(feedID int, url string, ctx context.Context) error {
log.Printf("Fetching %s...\n", url)
fp := gofeed.NewParser()
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
feed, err := fp.ParseURLWithContext(url, ctx)
if err != nil {
return fmt.Errorf("Failed to fetch %s: %v\n", url, err)
}
_, err = db.Exec(
`UPDATE feeds SET title = ?, fetched_at = ? WHERE id = ?`,
feed.Title,
time.Now().UTC().Format(time.RFC3339),
feedID,
)
if err != nil {
return err
}
rows, err := db.Query(`SELECT guid FROM articles WHERE feed_id = ?`, feedID)
if err != nil {
return err
}
defer rows.Close()
existingArticleGUIDs := make(map[string]bool)
for rows.Next() {
var guid string
err := rows.Scan(&guid)
if err != nil {
return err
}
existingArticleGUIDs[guid] = true
}
for _, item := range feed.Items {
if existingArticleGUIDs[item.GUID] {
_, err := db.Exec(
`UPDATE articles SET title = ?, url = ? WHERE feed_id = ? AND guid = ?`,
item.Title,
item.Link,
feedID,
item.GUID,
)
if err != nil {
return err
}
} else {
_, err := db.Exec(
`INSERT INTO articles (feed_id, guid, title, url, is_read) VALUES (?, ?, ?, ?, ?)`,
feedID,
item.GUID,
item.Title,
item.Link,
0,
)
if err != nil {
return err
}
}
}
return nil
}
func listFeedsToBeFetched() (map[int]string, error) {
rows, err := db.Query(`SELECT id, url, fetched_at FROM feeds`)
if err != nil {
return nil, err
}
defer rows.Close()
feeds := make(map[int]string)
for rows.Next() {
var feedID int
var url string
var fetchedAt string
err := rows.Scan(&feedID, &url, &fetchedAt)
if err != nil {
log.Fatal(err)
}
fetchedAtTime, err := time.Parse(time.RFC3339, fetchedAt)
if err != nil {
log.Fatal(err)
}
now := time.Now().UTC()
if now.Sub(fetchedAtTime).Minutes() <= 10 {
continue
}
feeds[feedID] = url
}
return feeds, nil
}
func fetchAllFeeds(ctx context.Context) error {
feeds, err := listFeedsToBeFetched()
if err != nil {
return err
}
var result *multierror.Error
for feedID, url := range feeds {
err := fetchOneFeed(feedID, url, ctx)
if err != nil {
result = multierror.Append(result, err)
}
time.Sleep(5 * time.Second)
}
return result.ErrorOrNil()
}
func getUnreadFeeds(c echo.Context) error {
rows, err := db.Query(`
SELECT a.id, a.title, f.url, f.title, f.id
FROM articles AS a
INNER JOIN feeds AS f ON a.feed_id = f.id
WHERE is_read = 0
ORDER BY a.id DESC
LIMIT 100
`)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
defer rows.Close()
return renderFeeds(c, "Unread feeds", "unread-feeds.html", rows)
}
func getReadFeeds(c echo.Context) error {
rows, err := db.Query(`
SELECT a.id, a.title, f.url, f.title, f.id
FROM articles AS a
INNER JOIN feeds AS f ON a.feed_id = f.id
WHERE is_read = 1
ORDER BY a.id DESC
LIMIT 100
`)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
defer rows.Close()
return renderFeeds(c, "Read feeds", "read-feeds.html", rows)
}
func renderFeeds(c echo.Context, title string, templateName string, rows *sql.Rows) error {
type Article struct {
ID int
Title string
URL string
}
type Feed struct {
ID int
URL string
Title string
Articles []Article
}
feeds := make(map[int]*Feed)
for rows.Next() {
var articleID int
var articleTitle string
var feedURL string
var feedTitle string
var feedID int
err := rows.Scan(&articleID, &articleTitle, &feedURL, &feedTitle, &feedID)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
if _, ok := feeds[feedID]; !ok {
feeds[feedID] = &Feed{
ID: feedID,
URL: feedURL,
Title: feedTitle,
}
}
feed := feeds[feedID]
feed.Articles = append(feed.Articles, Article{
ID: articleID,
Title: articleTitle,
URL: fmt.Sprintf("%s/articles/%d", basePath, articleID),
})
}
sortedFeeds := make([]*Feed, 0, len(feeds))
for _, feed := range feeds {
sortedFeeds = append(sortedFeeds, feed)
}
slices.SortFunc(sortedFeeds, func(a, b *Feed) int {
// Ascending order by URL.
if a.URL < b.URL {
return -1
} else if a.URL > b.URL {
return 1
} else {
return 0
}
})
return c.Render(http.StatusOK, templateName, struct {
BasePath string
Title string
Feeds []*Feed
}{
BasePath: basePath,
Title: title,
Feeds: sortedFeeds,
})
}
func getArticle(c echo.Context) error {
rawArticleID := c.Param("articleID")
articleID, err := strconv.Atoi(rawArticleID)
if err != nil {
return c.String(http.StatusNotFound, err.Error())
}
row := db.QueryRow(`SELECT url FROM articles WHERE id = ?`, articleID)
if row == nil {
return c.String(http.StatusNotFound, "Not found")
}
var url string
err = row.Scan(&url)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
// Turn is_read on.
_, err = db.Exec(`UPDATE articles SET is_read = 1 WHERE id = ?`, articleID)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
// Redirect to the article URL.
return c.Redirect(http.StatusFound, url)
}
func apiPutFeedRead(c echo.Context) error {
return apiPutFeed(c, "read")
}
func apiPutFeedUnread(c echo.Context) error {
return apiPutFeed(c, "unread")
}
func apiPutFeed(c echo.Context, op string) error {
rawFeedID := c.Param("feedID")
feedID, err := strconv.Atoi(rawFeedID)
if err != nil {
return c.String(http.StatusNotFound, err.Error())
}
// Turn is_read on or off.
var isReadValue int
if op == "read" {
isReadValue = 1
} else if op == "unread" {
isReadValue = 0
}
_, err = db.Exec(`UPDATE articles SET is_read = ? WHERE feed_id = ?`, isReadValue, feedID)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
return c.NoContent(http.StatusOK)
}
func apiPutArticleRead(c echo.Context) error {
return apiPutArticle(c, "read")
}
func apiPutArticleUnread(c echo.Context) error {
return apiPutArticle(c, "unread")
}
func apiPutArticle(c echo.Context, op string) error {
rawArticleID := c.Param("articleID")
articleID, err := strconv.Atoi(rawArticleID)
if err != nil {
return c.String(http.StatusNotFound, err.Error())
}
// Turn is_read on or off.
var isReadValue int
if op == "read" {
isReadValue = 1
} else if op == "unread" {
isReadValue = 0
}
_, err = db.Exec(`UPDATE articles SET is_read = ? WHERE id = ?`, isReadValue, articleID)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
return c.NoContent(http.StatusOK)
}
func scheduled(ctx context.Context, d time.Duration, fn func()) {
ticker := time.NewTicker(d)
go func() {
for {
select {
case <-ticker.C:
fn()
case <-ctx.Done():
return
}
}
}()
}
func main() {
basePath = os.Getenv("FEEDAKA_BASE_PATH")
port := os.Getenv("FEEDAKA_PORT")
var err error
db, err = sql.Open("sqlite3", "feedaka.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
err = initDB(db)
if err != nil {
log.Fatal(err)
}
t := &Template{
templates: template.Must(template.ParseFS(tmplFS, "templates/*.html")),
}
e := echo.New()
e.Renderer = t
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.GET("/", getIndex)
e.GET("/settings", getSettings)
e.POST("/settings", postSettings)
e.GET("/feeds/unread", getUnreadFeeds)
e.GET("/feeds/read", getReadFeeds)
e.GET("/articles/:articleID", getArticle)
e.PUT("/api/feeds/:feedID/read", apiPutFeedRead)
e.PUT("/api/feeds/:feedID/unread", apiPutFeedUnread)
e.PUT("/api/articles/:articleID/read", apiPutArticleRead)
e.PUT("/api/articles/:articleID/unread", apiPutArticleUnread)
e.GET("/static/*", echo.WrapHandler(http.FileServer(http.FS(staticFS))))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
scheduled(ctx, 1*time.Hour, func() {
err := fetchAllFeeds(ctx)
if err != nil {
log.Printf("Failed to fetch feeds: %v\n", err)
}
})
err = http.ListenAndServe(":"+port, http.StripPrefix(basePath, e))
log.Println(err)
}