/
miniflux-luma.go
97 lines (85 loc) · 2.5 KB
/
miniflux-luma.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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/gorilla/feeds"
"miniflux.app/client"
)
var miniflux *client.Client
var minifluxEndpoint string
var feedTitle string
func httpHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Security-Policy", "default-src 'none'; frame-ancestors 'none'; base-uri 'none'; form-action 'none'")
w.Header().Set("Referrer-Policy", "no-referrer")
w.Header().Set("X-Frame-Options", "DENY")
w.Header().Set("X-Content-Type-Options", "nosniff")
// Get new entries
entries, err := miniflux.Entries(&client.Filter{
Limit: 10,
Order: "published_at",
Direction: "desc",
Starred: "1",
})
if err != nil {
fmt.Println(err)
return
}
// Create atom feed
feed := &feeds.Feed{
Title: feedTitle,
Link: &feeds.Link{Href: minifluxEndpoint},
Created: time.Now(),
Items: []*feeds.Item{},
}
for _, entry := range entries.Entries {
feed.Items = append(feed.Items, &feeds.Item{
Title: entry.Title,
Link: &feeds.Link{Href: entry.URL},
Description: entry.Content,
Author: &feeds.Author{Name: entry.Author},
Created: entry.Date,
})
}
// Print atom feed
atom, err := feed.ToAtom()
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(w, atom)
}
func main() {
APITokenFile := ""
APIToken := ""
listenAddress := ""
certFile := ""
keyFile := ""
// Read command line arguments
flag.StringVar(&minifluxEndpoint, "endpoint", "https://miniflux.example.org", "Miniflux server endpoint")
flag.StringVar(&APITokenFile, "api-token-file", "api_token", "Load Miniflux API token from file")
flag.StringVar(&listenAddress, "listen-addr", "127.0.0.1:8080", "Listen on this address")
flag.StringVar(&feedTitle, "feed-title", "Starred entries", "Title of the Atom feed")
flag.StringVar(&certFile, "tls-cert", "", "TLS certificate file path (skip to disable TLS)")
flag.StringVar(&keyFile, "tls-key", "", "TLS key file path (skip to disable TLS)")
flag.Parse()
// Load API token
dat, err := os.ReadFile(APITokenFile)
if err != nil {
log.Fatal(err)
}
APIToken = strings.TrimSpace(string(dat))
// Authentication using API token then fetch starred items
miniflux = client.New(minifluxEndpoint, APIToken)
// Start web server
http.HandleFunc("/", httpHandler)
log.Printf("Listening on %s\n", listenAddress)
if certFile != "" && keyFile != "" {
log.Fatal(http.ListenAndServeTLS(listenAddress, certFile, keyFile, nil))
} else {
log.Fatal(http.ListenAndServe(listenAddress, nil))
}
}