-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathmain.go
611 lines (513 loc) · 14.6 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"regexp"
"sort"
"strings"
"time"
"github.com/getsentry/sentry-go"
sentrygin "github.com/getsentry/sentry-go/gin"
"github.com/gin-gonic/gin"
redis "github.com/go-redis/redis/v7"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/russross/blackfriday"
)
const (
zsetKey = "blogtopn"
)
var (
filenameRegex = regexp.MustCompile(`(\d{4}_\d{2}_\d{2})-.+\..+`)
articles = LoadMDs("articles")
db *sqlx.DB
redisClient *redis.Client
categoryMap = map[string]string{
"golang": "Golang简明教程",
"python": "Python教程",
"data_structure": "数据结构在实际项目中的使用",
}
// ErrNotFound means article not found
ErrNotFound = errors.New("Article Not Found")
// ErrFailedToLoad failed to load article
ErrFailedToLoad = errors.New("Failed To Load Article")
// Prometheus
totalRequests = promauto.NewCounter(prometheus.CounterOpts{Name: "total_requests_total"})
)
// InitSentry 初始化sentry
func InitSentry() error {
return sentry.Init(sentry.ClientOptions{
Dsn: os.Getenv("SENTRY_DSN"),
// Specify a fixed sample rate:
TracesSampleRate: 1.0,
})
}
// InitializeDB 初始化数据库连接
func InitializeDB() {
var err error
db, err = sqlx.Connect("mysql", os.Getenv("SQLX_URL"))
if err != nil {
log.Fatalf("failed to connect to the db: %s", err)
}
}
// InitializeRedis 初始化Redis
func InitializeRedis() {
opt, err := redis.ParseURL(os.Getenv("REDIS_URL"))
if err != nil {
log.Fatalf("failed to connect to redis db: %s", err)
}
// Create client as usually.
redisClient = redis.NewClient(opt)
}
// Article 就是文章
type Article struct {
Title string `json:"title"`
Date string `json:"date_str"`
Filename string `json:"file_name"`
DirName string `json:"dir_name"`
PubDate time.Time `json:"-"`
Description string `json:"description"`
}
// Articles 文章列表
type Articles []Article
func (a Articles) Len() int { return len(a) }
func (a Articles) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a Articles) Less(i, j int) bool {
v := strings.Compare(a[i].Date, a[j].Date)
if v <= 0 {
return true
}
return false
}
// RandomN return n articles by random
func (a Articles) RandomN(n int) Articles {
if n <= 0 {
return nil
}
length := len(a)
pos := rand.Intn(length - n)
return a[pos : pos+n]
}
func isBlogApp(c *gin.Context) bool {
ua := c.GetHeader("User-Agent")
if strings.HasPrefix(ua, "BlogApp/") {
return true
}
return false
}
func getFilePath(path string) string {
suffix := ".html"
if strings.HasSuffix(path, suffix) {
path = path[:len(path)-len(suffix)]
}
return "./" + path
}
// ReadDesc 把简介读出来
func ReadDesc(path string) string {
path = getFilePath(path)
file, err := os.Open(path)
if err != nil {
log.Printf("failed to read file(%s): %s", path, err)
return ""
}
reader := bufio.NewReader(file)
reader.ReadLine() // 忽略第一行(标题)
reader.ReadLine() // 忽略第二行(空行)
desc := ""
for i := 0; i < 3; i++ {
line, _, err := reader.ReadLine()
if err != nil && err != io.EOF {
log.Printf("failed to read desc of file(%s): %s", path, err)
continue
}
desc += string(line)
}
trimChars := "\n,。:,.:"
return strings.TrimRight(strings.TrimLeft(desc, trimChars), trimChars) + "..."
}
// ReadTitle 把标题读出来
func ReadTitle(path string) string {
path = getFilePath(path)
file, err := os.Open(path)
if err != nil {
log.Printf("failed to read file(%s): %s", path, err)
return ""
}
line, _, err := bufio.NewReader(file).ReadLine()
if err != nil {
log.Printf("failed to read title of file(%s): %s", path, err)
return ""
}
title := strings.Replace(string(line), "# ", "", -1)
return title
}
// VisitedArticle is for remember which article had been visited
type VisitedArticle struct {
URLPath string `json:"url_path"`
Title string `json:"title"`
}
func genVisited(urlPath, subTitle string) (string, error) {
title := ReadTitle(urlPath)
if title == "" {
return "", ErrNotFound
}
if subTitle != "" {
title += " - " + subTitle
}
visited := VisitedArticle{URLPath: urlPath, Title: title}
b, err := json.Marshal(visited)
if err != nil {
return "", ErrFailedToLoad
}
return string(b), nil
}
func getTopVisited(n int) []VisitedArticle {
visitedArticles := []VisitedArticle{}
articles, err := redisClient.ZRevRangeByScore(zsetKey, &redis.ZRangeBy{
Min: "-inf", Max: "+inf", Offset: 0, Count: int64(n),
}).Result()
if err != nil {
log.Printf("failed to get top %d visited articles: %s", n, err)
return nil
}
for _, article := range articles {
var va VisitedArticle
if err := json.Unmarshal([]byte(article), &va); err != nil {
log.Printf("failed to unmarshal article: %s", err)
continue
}
visitedArticles = append(visitedArticles, va)
}
return visitedArticles
}
// LoadArticle 把文章的元信息读出来
func LoadArticle(dirname, filename string) *Article {
match := filenameRegex.FindStringSubmatch(filename)
if len(match) != 2 {
return nil
}
dateString := strings.Replace(match[1], "_", "-", -1)
filepath := fmt.Sprintf("./%s/%s", dirname, filename)
title := ReadTitle(filepath)
pubDate, err := time.Parse("2006-01-02", dateString)
if err != nil {
log.Panicf("failed to parse date: %s", err)
}
desc := ReadDesc(filepath)
return &Article{
Title: title,
Date: dateString,
Filename: filename,
DirName: dirname,
PubDate: pubDate,
Description: desc,
}
}
// LoadMDs 读取给定目录中的所有markdown文章
func LoadMDs(dirname string) Articles {
files, err := ioutil.ReadDir(dirname)
if err != nil {
log.Fatalf("failed to read dir(%s): %s", dirname, err)
return nil
}
var articles Articles
for _, file := range files {
filename := file.Name()
if article := LoadArticle(dirname, filename); article != nil {
articles = append(articles, *article)
}
}
sort.Sort(sort.Reverse(articles))
return articles
}
// IndexHandler 首页
func IndexHandler(c *gin.Context) {
topArticles := getTopVisited(15)
c.HTML(
http.StatusOK, "index.html", gin.H{
"isBlogApp": isBlogApp(c),
"articles": articles[:100],
"totalCount": len(articles),
"keywords": "Golang,Python,Go语言,Dart,Android,安卓,Kotlin,分布式,高并发,Haskell,C,微服务,软件工程,源码阅读,源码分析",
"description": "享受技术带来的快乐~分布式系统/高并发处理/Golang/Python/Haskell/C/微服务/Android/安卓/Kotlin/软件工程/源码阅读与分析",
"topArticles": topArticles,
},
)
}
// ArchiveHandler 全部文章
func ArchiveHandler(c *gin.Context) {
c.HTML(
http.StatusOK, "index.html", gin.H{
"isBlogApp": isBlogApp(c),
"articles": articles,
"keywords": "Golang,Python,Go语言,Dart,Android,安卓,Kotlin,分布式,高并发,Haskell,C,微服务,软件工程,源码阅读,源码分析",
"description": "享受技术带来的快乐~分布式系统/高并发处理/Golang/Python/Haskell/C/微服务/Android/安卓/Kotlin/软件工程/源码阅读与分析",
},
)
}
func renderArticle(c *gin.Context, status int, path string, subtitle string, randomN int) {
path = getFilePath(path)
content, err := ioutil.ReadFile(path)
if err != nil {
log.Printf("failed to read file %s: %s", path, err)
c.Redirect(http.StatusFound, "/404")
return
}
content = blackfriday.MarkdownCommon(content)
recommends := articles.RandomN(randomN)
topArticles := getTopVisited(15)
c.HTML(
status, "article.html", gin.H{
"isBlogApp": isBlogApp(c),
"content": template.HTML(content),
"title": ReadTitle(path),
"subtitle": subtitle,
"recommends": recommends,
"topArticles": topArticles,
},
)
}
func incrVisited(urlPath, subTitle string) {
if visited, err := genVisited(urlPath, subTitle); err != nil {
log.Printf("failed to gen visited: %s", err)
} else {
if _, err := redisClient.ZIncrBy(zsetKey, 1, visited).Result(); err != nil {
log.Printf("failed to incr score of %s: %s", urlPath, err)
}
}
}
// PingPongHandler ping pong
func PingPongHandler(c *gin.Context) {
c.JSON(http.StatusOK, nil)
}
// ArticleHandler 具体文章
func ArticleHandler(c *gin.Context) {
urlPath := c.Request.URL.Path
incrVisited(urlPath, "")
renderArticle(c, http.StatusOK, urlPath, "", 15)
}
// TutorialPageHandler 教程index
func TutorialPageHandler(c *gin.Context) {
renderArticle(c, http.StatusOK, "articles/tutorial.md", "", 0)
}
// AboutMeHandler 关于我
func AboutMeHandler(c *gin.Context) {
renderArticle(c, http.StatusOK, "articles/aboutme.md", "", 0)
}
// FriendsHandler 友链
func FriendsHandler(c *gin.Context) {
renderArticle(c, http.StatusOK, "articles/friends.md", "", 0)
}
// AppHandler App页面
func AppHandler(c *gin.Context) {
renderArticle(c, http.StatusOK, "articles/app.md", "", 0)
}
// NotFoundHandler 404
func NotFoundHandler(c *gin.Context) {
renderArticle(c, http.StatusOK, "articles/404.md", "", 20)
}
// AllSharingHandler 所有分享
func AllSharingHandler(c *gin.Context) {
sharing := dao.GetAllSharing()
c.HTML(
http.StatusOK, "list.html", gin.H{
"isBlogApp": isBlogApp(c),
"sharing": sharing,
},
)
}
// SharingHandler 分享
func SharingHandler(c *gin.Context) {
sharing := dao.GetSharingWithLimit(20)
c.HTML(
http.StatusOK, "list.html", gin.H{
"isBlogApp": isBlogApp(c),
"sharing": sharing,
"partly": true,
},
)
}
// NotesHandler 随想
func NotesHandler(c *gin.Context) {
notes := dao.GetAllNotes()
c.HTML(
http.StatusOK, "list.html", gin.H{
"isBlogApp": isBlogApp(c),
"notes": notes,
},
)
}
// RSSHandler RSS
func RSSHandler(c *gin.Context) {
c.Header("Content-Type", "application/xml")
c.HTML(
http.StatusOK, "rss.html", gin.H{
"isBlogApp": isBlogApp(c),
"rssHeader": template.HTML(`<?xml version="1.0" encoding="UTF-8"?>`),
"articles": articles,
},
)
}
// SharingRSSHandler RSS for sharing channel
func SharingRSSHandler(c *gin.Context) {
sharings := dao.GetAllSharing()
c.Header("Content-Type", "application/xml")
c.HTML(
http.StatusOK, "sharing_rss.html", gin.H{
"isBlogApp": isBlogApp(c),
"rssHeader": template.HTML(`<?xml version="1.0" encoding="UTF-8"?>`),
"sharings": sharings,
},
)
}
// SiteMapHandler sitemap
func SiteMapHandler(c *gin.Context) {
c.Header("Content-Type", "application/xml")
c.HTML(
http.StatusOK, "sitemap.html", gin.H{
"isBlogApp": isBlogApp(c),
"rssHeader": template.HTML(`<?xml version="1.0" encoding="UTF-8"?>`),
"articles": articles,
},
)
}
// TutorialHandler 教程
func TutorialHandler(c *gin.Context) {
category := c.Param("category")
filename := c.Param("filename")
urlPath := c.Request.URL.Path
subTitle := categoryMap[category]
incrVisited(urlPath, subTitle)
renderArticle(c, http.StatusOK, fmt.Sprintf("tutorial/%s/%s", category, filename), subTitle, 15)
}
// SearchHandler 搜索
func SearchHandler(c *gin.Context) {
word := c.PostForm("search")
c.Redirect(
http.StatusFound,
"https://www.google.com/search?q=site:jiajunhuang.com "+word,
)
}
// RewardHandler 扫码赞赏
func RewardHandler(c *gin.Context) {
userAgent := c.Request.UserAgent()
if strings.Contains(userAgent, "MicroMessenger") {
c.Redirect(http.StatusFound, os.Getenv("WECHAT_PAY_URL"))
return
}
c.Redirect(http.StatusFound, os.Getenv("ALIPAY_URL"))
}
// ArticlesAPIHandler 首页文章API
func ArticlesAPIHandler(c *gin.Context) {
queryObj := struct {
Page int `form:"page,default=1"`
}{}
if err := c.BindQuery(&queryObj); err != nil {
log.Printf("failed to bind page: %s", err)
}
perPage := 50
start := (queryObj.Page - 1) * perPage
if start < 0 {
start = 0
}
if start > len(articles) {
start = len(articles)
}
end := start + perPage
if end > len(articles) {
end = len(articles)
}
c.JSON(http.StatusOK, gin.H{"msg": "", "result": articles[start:end]})
}
// TopArticlesAPIHandler 热门文章API
func TopArticlesAPIHandler(c *gin.Context) {
topArticles := getTopVisited(20)
c.JSON(http.StatusOK, gin.H{"msg": "", "result": topArticles})
}
// SharingAPIHandler 获取分享
func SharingAPIHandler(c *gin.Context) {
queryObj := struct {
Page int `form:"page,default=1"`
}{}
if err := c.BindQuery(&queryObj); err != nil {
log.Printf("failed to bind page: %s", err)
}
limit := 50
offset := (queryObj.Page - 1) * limit
sharings := dao.GetSharing(limit, offset)
c.JSON(http.StatusOK, gin.H{"msg": "", "result": sharings})
}
// NotesAPIHandler 获取随想
func NotesAPIHandler(c *gin.Context) {
queryObj := struct {
Page int `form:"page,default=1"`
}{}
if err := c.BindQuery(&queryObj); err != nil {
log.Printf("failed to bind page: %s", err)
}
limit := 50
offset := (queryObj.Page - 1) * limit
notes := dao.GetNotes(limit, offset)
c.JSON(http.StatusOK, gin.H{"msg": "", "result": notes})
}
func main() {
if err := InitSentry(); err != nil {
log.Panicf("failed to init sentry: %s", err)
}
defer sentry.Flush(2 * time.Second)
// telegram bot
go startNoteBot()
go startSharingBot()
InitializeDB()
InitializeRedis()
r := gin.New()
r.Use(sentrygin.New(sentrygin.Options{}))
r.Use(gin.Logger())
r.Use(func(c *gin.Context) {
totalRequests.Inc()
})
r.LoadHTMLGlob("templates/*.html")
r.Static("/static", "./static")
//r.Static("/tutorial/:lang/img/", "./tutorial/:lang/img") # 然而不支持
//r.Static("/articles/img", "./articles/img") # 然而有冲突
r.StaticFile("/favicon.ico", "./static/favicon.ico")
r.StaticFile("/robots.txt", "./static/robots.txt")
r.StaticFile("/ads.txt", "./static/ads.txt")
r.GET("/", IndexHandler)
r.GET("/ping", PingPongHandler)
r.GET("/404", NotFoundHandler)
r.GET("/archive", ArchiveHandler)
r.GET("/articles/:filepath", ArticleHandler)
r.GET("/aboutme", AboutMeHandler)
r.GET("/tutorial", TutorialPageHandler)
r.GET("/friends", FriendsHandler)
r.GET("/app", AppHandler)
r.GET("/sharing", SharingHandler)
r.GET("/sharing/all", AllSharingHandler)
r.GET("/sharing/rss", SharingRSSHandler)
r.GET("/notes", NotesHandler)
r.GET("/api/v1/articles", ArticlesAPIHandler)
r.GET("/api/v1/topn", TopArticlesAPIHandler)
r.GET("/api/v1/sharing", SharingAPIHandler)
r.GET("/api/v1/notes", NotesAPIHandler)
r.GET("/rss", RSSHandler)
r.GET("/sitemap.xml", SiteMapHandler)
r.GET("/tutorial/:category/:filename", TutorialHandler)
r.GET("/reward", RewardHandler)
r.POST("/search", SearchHandler)
r.GET("/metrics", gin.WrapH(promhttp.Handler()))
r.NoRoute(func(c *gin.Context) { c.Redirect(http.StatusFound, "/404") })
r.Run("0.0.0.0:8080")
}