-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
139 lines (125 loc) · 3.22 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
package main
import (
"flag"
"html/template"
"log"
"net/http"
"net/url"
"os"
"github.com/PuerkitoBio/goquery"
"github.com/gorilla/feeds"
"gopkg.in/yaml.v3"
)
type FeedSpec struct {
Title string `yaml:"title"`
Description string `yaml:"description"`
Link string `yaml:"link"`
Spec struct {
Item string `yaml:"item"`
Title string `yaml:"title"`
Description string `yaml:"description"`
Link string `yaml:"link"`
} `yaml:"spec"`
}
type Config struct {
Listen string `yaml:"listen"`
Feeds map[string]FeedSpec `yaml:"feeds"`
}
var (
configPath = flag.String("config", "", "path to config file")
config = Config{Listen: "127.0.0.1:9977"}
)
func handleHome(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/html; charset=utf8")
t := template.Must(template.New("home").Parse(`<!doctype html>
<html>
<h2>Feeds</h2>
<ul>
{{range $slug, $feed := .}}
<li><a href="/feeds/{{$slug}}">{{$feed.Title}}</a></li>
{{end}}
</ul>
</html>
`))
t.Execute(w, config.Feeds)
}
func handleFeeds(w http.ResponseWriter, r *http.Request) {
id := r.URL.Path[len("/feeds/"):]
feedSpec, ok := config.Feeds[id]
if !ok {
w.WriteHeader(http.StatusNotFound)
return
}
res, err := http.Get(feedSpec.Link)
if err != nil {
w.WriteHeader(http.StatusBadGateway)
log.Printf("error fetching feed %s: %s", id, err)
return
}
defer res.Body.Close()
if res.StatusCode != 200 {
w.WriteHeader(http.StatusBadGateway)
log.Printf("bad status code fetching feed %s: %d", id, res.StatusCode)
return
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("error parsing the feed %s: %s", id, err)
return
}
feed := &feeds.Feed{
Title: feedSpec.Title,
Link: &feeds.Link{Href: feedSpec.Link},
Description: feedSpec.Description,
Items: []*feeds.Item{},
}
base, err := url.Parse(feedSpec.Link)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("error parsing the feed %s: %s", id, err)
return
}
doc.Find(feedSpec.Spec.Item).Each(func(i int, s *goquery.Selection) {
var link string
desc, _ := s.Find(feedSpec.Spec.Description).Html()
u, err := base.Parse(s.Find(feedSpec.Spec.Link).AttrOr("href", ""))
if err != nil {
link = "failed to parse link: err"
} else {
link = u.String()
}
feed.Items = append(feed.Items, &feeds.Item{
Title: s.Find(feedSpec.Spec.Title).Text(),
Link: &feeds.Link{Href: link},
Description: desc,
})
})
w.Header().Add("Content-Type", "application/rss+xml; charset=utf8")
feed.WriteRss(w)
}
func main() {
log.SetFlags(0)
flag.Parse()
if *configPath != "" {
var f *os.File
var err error
if *configPath == "-" {
f = os.Stdin
} else {
f, err = os.Open(*configPath)
}
if err != nil {
log.Fatalf("failed to open %s: %s", *configPath, err)
}
y := yaml.NewDecoder(f)
if err = y.Decode(&config); err != nil {
log.Fatalf("failed to load config %s: %s", *configPath, err)
}
f.Close()
}
log.Printf("listening on http://%s/", config.Listen)
http.HandleFunc("/", handleHome)
http.HandleFunc("/feeds/", handleFeeds)
log.Fatal(http.ListenAndServe(config.Listen, nil))
}