-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmain.go
More file actions
289 lines (259 loc) · 9.18 KB
/
main.go
File metadata and controls
289 lines (259 loc) · 9.18 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
Copyright 2024 The west2-online Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/cloudwego/kitex/pkg/limit"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/server"
"github.com/cloudwego/netpoll"
etcd "github.com/kitex-contrib/registry-etcd"
"github.com/west2-online/fzuhelper-server/config"
"github.com/west2-online/fzuhelper-server/internal/common"
"github.com/west2-online/fzuhelper-server/internal/common/pack"
"github.com/west2-online/fzuhelper-server/kitex_gen/common/commonservice"
"github.com/west2-online/fzuhelper-server/pkg/base"
"github.com/west2-online/fzuhelper-server/pkg/cache"
"github.com/west2-online/fzuhelper-server/pkg/constants"
"github.com/west2-online/fzuhelper-server/pkg/db"
"github.com/west2-online/fzuhelper-server/pkg/db/model"
"github.com/west2-online/fzuhelper-server/pkg/github"
"github.com/west2-online/fzuhelper-server/pkg/logger"
"github.com/west2-online/fzuhelper-server/pkg/taskqueue"
"github.com/west2-online/fzuhelper-server/pkg/umeng"
"github.com/west2-online/fzuhelper-server/pkg/upyun"
"github.com/west2-online/fzuhelper-server/pkg/utils"
"github.com/west2-online/jwch"
)
var (
serviceName = constants.CommonServiceName
clientSet *base.ClientSet
taskQueue taskqueue.TaskQueue
)
func init() {
config.Init(serviceName)
logger.Init(serviceName, config.GetLoggerLevel())
clientSet = base.NewClientSet(base.WithDBClient(), base.WithRedisClient(constants.RedisDBCommon))
taskQueue = taskqueue.NewBaseTaskQueue()
loadNotice(clientSet.DBClient)
}
// TODO: 失败后的重试机制
func loadNotice(db *db.Database) {
stu := jwch.NewStudent().WithUser(config.DefaultUser.Account, config.DefaultUser.Password)
_, totalPage, err := stu.GetNoticeInfo(&jwch.NoticeInfoReq{PageNum: 1})
if err != nil {
logger.Errorf("syncer init: failed to get notice info: %v", err)
}
// 初始化数据库
for i := 1; i <= totalPage; i++ {
content, _, err := stu.GetNoticeInfo(&jwch.NoticeInfoReq{PageNum: i})
if err != nil {
logger.Errorf("syncer init: failed to get notice info in page %d: %v", i, err)
}
for _, row := range content {
ctx := context.Background()
info := &model.Notice{
Title: row.Title,
PublishedAt: row.Date,
URL: row.URL,
}
err = db.Notice.CreateNotice(ctx, info)
if err != nil {
logger.Warnf("syncer init: failed to create notice in page %d: %v", i, err)
}
}
}
logger.Infof("syncer init: notice syncer init success")
}
func main() {
var watcherCancel context.CancelFunc
if os.Getenv(constants.DeployEnv) != "k8s" {
watcherCtx, cancel := context.WithCancel(context.Background())
watcherCancel = cancel
go config.StartEtcdWatcher(watcherCtx, serviceName)
}
r, err := etcd.NewEtcdRegistry([]string{config.Etcd.Addr})
if err != nil {
logger.Fatalf("Common: etcd registry failed, error: %v", err)
}
listenAddr, err := utils.GetAvailablePort()
if err != nil {
logger.Fatalf("Common: get available port failed: %v", err)
}
addr, err := netpoll.ResolveTCPAddr("tcp", listenAddr)
if err != nil {
logger.Fatalf("Common: listen addr failed %v", err)
}
svr := commonservice.NewServer(
common.NewCommonService(clientSet),
server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{
ServiceName: serviceName,
}),
server.WithMuxTransport(),
server.WithServiceAddr(addr),
server.WithRegistry(r),
server.WithLimit(&limit.Option{
MaxConnections: constants.MaxConnections,
MaxQPS: constants.MaxQPS,
}),
)
server.RegisterShutdownHook(func() {
if watcherCancel != nil {
logger.Info("Shutting down etcd config watcher...")
watcherCancel()
}
logger.Info("Closing client resources...")
clientSet.Close()
})
taskQueue.AddSchedule(constants.NoticeTaskKey, taskqueue.ScheduleQueueTask{
Execute: syncNoticeTask,
GetScheduleTime: func() time.Duration {
return constants.NoticeUpdateTime
},
})
taskQueue.AddSchedule(constants.ContributorTaskKey, taskqueue.ScheduleQueueTask{
Execute: syncContributorTask,
GetScheduleTime: func() time.Duration {
return constants.ContributorInfoUpdateTime
},
})
taskQueue.Start()
if err = svr.Run(); err != nil {
logger.Fatalf("Common: server run failed: %v", err)
}
}
func syncNoticeTask() error {
logger.Infof("syncNoticeTask: jwch notice sync task started")
// 默认爬取第一页的内容(教务处不太可能一次性更新出一页的数据),然后和数据库做 diff 操作
content, _, err := jwch.NewStudent().WithUser(config.DefaultUser.Account, config.DefaultUser.Password).GetNoticeInfo(&jwch.NoticeInfoReq{PageNum: 1})
if err != nil {
logger.Errorf("notice sync task: failed to get notice info: %v", err)
return fmt.Errorf("failed to get notice info: %w", err)
}
for _, row := range content {
// 判断是否已存在
ctx := context.Background()
ok, err := clientSet.DBClient.Notice.IsNoticeExists(ctx, row.Title, row.URL)
if err != nil {
return fmt.Errorf("notice sync task: failed to check url exists: %w", err)
}
// 数据库已存在,无需处理
if ok {
continue
}
info := &model.Notice{
Title: row.Title,
URL: row.URL,
PublishedAt: row.Date,
}
if err = clientSet.DBClient.Notice.CreateNotice(ctx, info); err != nil {
return fmt.Errorf("notice sync task: failed to create notice: %w", err)
}
channelProperties := map[string]string{
"channel_activity": "com.west2online.umeng.MfrMessageActivity",
"huawei_channel_importance": "NORMAL",
"xiaomi_channel_id": config.Vendors.Xiaomi.JwchNotice,
}
// 进行消息推送
err = umeng.SendAndroidGroupcastWithUrl(config.Umeng.Android.AppKey, config.Umeng.Android.AppMasterSecret,
"", "教务处通知", info.Title, constants.UmengJwchNoticeTag, info.URL, channelProperties)
if err != nil {
logger.Errorf("notice sync task: failed to send notice to Android: %v", err)
}
err = umeng.SendIOSGroupcast(config.Umeng.IOS.AppKey, config.Umeng.IOS.AppMasterSecret,
"教务处通知", "", info.Title, constants.UmengJwchNoticeTag)
if err != nil {
logger.Errorf("notice sync task: failed to send notice to IOS: %v", err)
}
logger.Infof("notice sync task: notice send success")
}
return nil
}
func syncContributorTask() error {
logger.Info("syncContributorTask: contributor info sync task started")
urls := []string{
constants.ContributorFzuhelperApp,
constants.ContributorFzuhelperServer,
constants.ContributorJwch,
constants.ContributorYJSY,
}
contributorKeys := []string{
constants.ContributorFzuhelperAppKey,
constants.ContributorFzuhelperServerKey,
constants.ContributorJwchKey,
constants.ContributorYJSYKey,
}
for i, url := range urls {
rawContributors, err := github.FetchContributorsFromURL(url)
if err != nil {
return fmt.Errorf("contributor info sync: failed to fetch contributors from %s: %w", url, err)
}
contributors := pack.BuildContributors(rawContributors)
for i, contributor := range contributors {
newAvatarUrl, err := uploadAvatar(contributor.AvatarUrl, contributor.Name)
if err != nil {
return fmt.Errorf("contributor info sync: failed to upload avatar for contributor %s: %w", contributor.Name, err)
}
// 替换头像 url
contributors[i].AvatarUrl = newAvatarUrl
}
if err := cache.SetSliceCache(clientSet.CacheClient, context.Background(),
contributorKeys[i], contributors,
constants.KeyNeverExpire, "Common.SyncContributorInfo"); err != nil {
return fmt.Errorf("contributor info sync: failed to cache contributors: %w", err)
}
}
return nil
}
const (
baseUrl = "https://avatars.githubusercontent.com/u/"
uploadBase = "http://v0.api.upyun.com/fzuhelper-filedown"
readBase = "https://download.w2fzu.com"
)
func uploadAvatar(avatarUrl string, name string) (string, error) {
if strings.HasPrefix(avatarUrl, baseUrl) {
// 1.将原始 URL 替换成反代 URL
parsedUrl, err := url.Parse(avatarUrl)
if err != nil {
return "", err
}
// parsedUrl.Path[3:]会去掉 `/u/`
newAvatarUrl := fmt.Sprintf(constants.AvatarProxy, parsedUrl.Path[3:])
// 2.下载图片并上传又拍云
resp, err := http.Get(newAvatarUrl)
if err != nil {
return "", fmt.Errorf("failed to download avatar from %s: %w", avatarUrl, err)
}
imgData, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read avatar image: %w", err)
}
// 生成上传用Url
newAvatarUrl = upyun.GenerateContributorAvatarUrl(name)
err = upyun.URlUploadFile(imgData, newAvatarUrl)
if err != nil {
return "", fmt.Errorf("failed to upload avatar to image host: %w", err)
}
_ = resp.Body.Close()
// 3.最终换成加速域名
return strings.Replace(newAvatarUrl, uploadBase, readBase, 1), nil
}
return "", nil
}