-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrssc.go
77 lines (62 loc) · 1.51 KB
/
rssc.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
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
_ "embed"
"github.com/larrasket/rssc/filter"
"github.com/patrickmn/go-cache"
)
//go:embed README.html
var README string
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(README))
})
c := cache.New(5*time.Second, 20*time.Second)
http.HandleFunc("/rss", func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
if req, found := c.Get(q.Encode()); found {
res := req.(string)
w.Write([]byte(res))
return
}
prams := filter.FilterPrams{AuthorRegex: q.Get("authorf"),
ContentRegex: q.Get("contentf"),
TitleRegex: q.Get("titlef"),
DescriptionRegex: q.Get("descriptionf"),
LinkRegex: q.Get("linkf"),
URL: q.Get("src"),
DotNet: func(b string) bool {
if b == "1" {
return true
}
return false
}(q.Get("net")),
}
entries, err := filter.FilterFeeds(&prams)
if err != nil {
w.Write([]byte(err.Error()))
}
ftype := q.Get("t")
feeds, err := filter.GenerateFeeds(entries, ftype)
if err != nil {
w.Write([]byte(err.Error()))
}
if ftype == "json" {
w.Header().Set("Content-Type", "application/json")
} else {
w.Header().Set("Content-Type", "application/xml")
}
c.Set(q.Encode(), feeds, cache.DefaultExpiration)
w.Write([]byte(feeds))
})
port, ok := os.LookupEnv("PORT")
if ok == false {
port = "8080"
}
fmt.Printf("Listening on :%s...\n", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}