-
Notifications
You must be signed in to change notification settings - Fork 74
/
main.go
147 lines (130 loc) · 3.42 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
package main
import (
"encoding/json"
"html/template"
"log"
"net/http"
"rss-reader/globals"
"rss-reader/models"
"rss-reader/utils"
"time"
"github.com/gorilla/websocket"
)
func init() {
globals.Init()
}
func main() {
go utils.UpdateFeeds()
go utils.WatchConfigFileChanges("config.json")
http.HandleFunc("/feeds", getFeedsHandler)
http.HandleFunc("/ws", wsHandler)
// http.HandleFunc("/", serveHome)
http.HandleFunc("/", tplHandler)
//加载静态文件
fs := http.FileServer(http.FS(globals.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(globals.HtmlContent)
}
func tplHandler(w http.ResponseWriter, r *http.Request) {
// 创建一个新的模板,并设置自定义分隔符为<< >>,避免与Vue的语法冲突
tmplInstance := template.New("index.html").Delims("<<", ">>")
//添加加法函数计数
funcMap := template.FuncMap{
"inc": func(i int) int {
return i + 1
},
}
// 加载模板文件
tmpl, err := tmplInstance.Funcs(funcMap).ParseFS(globals.DirStatic, "static/index.html")
if err != nil {
log.Println("模板加载错误:", err)
return
}
//判断现在是否是夜间
formattedTime := time.Now().Format("15:04:05")
darkMode := false
if globals.RssUrls.NightStartTime != "" && globals.RssUrls.NightEndTime != "" {
if globals.RssUrls.NightStartTime > formattedTime || formattedTime > globals.RssUrls.NightEndTime {
darkMode = true
}
}
// 定义一个数据对象
data := struct {
Keywords string
RssDataList []models.Feed
DarkMode bool
AutoUpdatePush int
}{
Keywords: getKeywords(),
RssDataList: utils.GetFeeds(),
DarkMode: darkMode,
AutoUpdatePush: globals.RssUrls.AutoUpdatePush,
}
// 渲染模板并将结果写入响应
err = tmpl.Execute(w, data)
if err != nil {
log.Println("模板渲染错误:", err)
}
}
func wsHandler(w http.ResponseWriter, r *http.Request) {
conn, err := globals.Upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("Upgrade failed: %v", err)
return
}
defer conn.Close()
for {
for _, url := range globals.RssUrls.Values {
globals.Lock.RLock()
cache, ok := globals.DbMap[url]
globals.Lock.RUnlock()
if !ok {
log.Printf("Error getting feed from db is null %v", url)
continue
}
data, err := json.Marshal(cache)
if err != nil {
log.Printf("json marshal failure: %s", err.Error())
continue
}
err = conn.WriteMessage(websocket.TextMessage, data)
//错误直接关闭更新
if err != nil {
log.Printf("Error sending message or Connection closed: %v", err)
return
}
}
//如果未配置则不自动更新
if globals.RssUrls.AutoUpdatePush == 0 {
return
}
time.Sleep(time.Duration(globals.RssUrls.AutoUpdatePush) * time.Minute)
}
}
//获取关键词也就是title
//获取feeds列表
func getKeywords() string {
words := ""
for _, url := range globals.RssUrls.Values {
globals.Lock.RLock()
cache, ok := globals.DbMap[url]
globals.Lock.RUnlock()
if !ok {
log.Printf("Error getting feed from db is null %v", url)
continue
}
if cache.Title != "" {
words += cache.Title + ","
}
}
return words
}
func getFeedsHandler(w http.ResponseWriter, r *http.Request) {
feeds := utils.GetFeeds()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(feeds)
}