forked from srcrs/rss-reader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
144 lines (126 loc) · 3.11 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
package main
import (
"embed"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"sync"
"time"
"github.com/gorilla/websocket"
"github.com/mmcdole/gofeed"
)
type Config struct {
Values []string `json:"values"`
ReFresh int `json:"refresh"`
AutoUpdatePush int `json:"autoUpdatePush"`
}
var (
dbMap sync.Map
rssUrls Config
upgrader = websocket.Upgrader{}
//go:embed static
dirStatic embed.FS
//go:embed index.html
fileIndex embed.FS
htmlContent []byte
)
func init() {
// 读取配置文件
data, err := ioutil.ReadFile("config.json")
if err != nil {
panic(err)
}
// 解析JSON数据到Config结构体
err = json.Unmarshal(data, &rssUrls)
if err != nil {
panic(err)
}
// 读取 index.html 内容
htmlContent, err = fileIndex.ReadFile("index.html")
if err != nil {
panic(err)
}
}
func main() {
go updateFeeds()
http.HandleFunc("/feeds", getFeedsHandler)
http.HandleFunc("/ws", wsHandler)
http.HandleFunc("/", serveHome)
//加载静态文件
fs := http.FileServer(http.FS(dirStatic))
http.Handle("/static/", fs)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func serveHome(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/html; charset=utf-8")
w.Write(htmlContent)
}
func wsHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("Upgrade failed: %v", err)
return
}
defer conn.Close()
for {
for _, url := range rssUrls.Values {
feedJSON, ok := dbMap.Load(url)
if !ok {
log.Printf("Error getting feed from db is null %v", url)
continue
}
err = conn.WriteMessage(websocket.TextMessage, []byte(feedJSON.(string)))
//错误直接关闭更新
if err != nil {
log.Printf("Error sending message or Connection closed: %v", err)
return
}
}
//如果未配置则不自动更新
if rssUrls.AutoUpdatePush == 0 {
return
}
time.Sleep(time.Duration(rssUrls.AutoUpdatePush) * time.Minute)
}
}
func updateFeeds() {
for {
for _, url := range rssUrls.Values {
fp := gofeed.NewParser()
feed, err := fp.ParseURL(url)
if err != nil {
log.Printf("Error fetching feed: %v | %v", url, err)
continue
}
currentTime := time.Now()
formattedTime := currentTime.Format("2006-01-02 15:04:05")
feed.Custom = map[string]string{"lastupdate": formattedTime}
feedJSON, err := json.Marshal(feed)
if err != nil {
log.Printf("Error marshaling feed: %v", err)
continue
}
dbMap.Store(url, string(feedJSON))
}
time.Sleep(time.Duration(rssUrls.ReFresh) * time.Minute)
}
}
func getFeedsHandler(w http.ResponseWriter, r *http.Request) {
feeds := make([]gofeed.Feed, 0, len(rssUrls.Values))
for _, url := range rssUrls.Values {
feedJSON, ok := dbMap.Load(url)
if !ok {
log.Printf("Error getting feed from db is null %v", url)
continue
}
var feed gofeed.Feed
if err := json.Unmarshal([]byte(feedJSON.(string)), &feed); err != nil {
log.Printf("Error unmarshaling feed: %v", err)
continue
}
feeds = append(feeds, feed)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(feeds)
}