-
Notifications
You must be signed in to change notification settings - Fork 281
/
index.go
71 lines (66 loc) · 1.91 KB
/
index.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
package controllers
import (
"net/http"
"strconv"
"math"
"github.com/gin-gonic/gin"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday"
"github.com/wangsongyan/wblog/models"
"github.com/wangsongyan/wblog/system"
)
func IndexGet(c *gin.Context) {
var (
pageIndex int
pageSize = system.GetConfiguration().PageSize
total int
page string
err error
posts []*models.Post
policy *bluemonday.Policy
)
page = c.Query("page")
pageIndex, _ = strconv.Atoi(page)
if pageIndex <= 0 {
pageIndex = 1
}
posts, err = models.ListPublishedPost("", pageIndex, pageSize)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
total, err = models.CountPostByTag("")
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
policy = bluemonday.StrictPolicy()
for _, post := range posts {
post.Tags, _ = models.ListTagByPostId(strconv.FormatUint(uint64(post.ID), 10))
post.Body = policy.Sanitize(string(blackfriday.MarkdownCommon([]byte(post.Body))))
}
user, _ := c.Get(CONTEXT_USER_KEY)
c.HTML(http.StatusOK, "index/index.html", gin.H{
"posts": posts,
"tags": models.MustListTag(),
"archives": models.MustListPostArchives(),
"links": models.MustListLinks(),
"user": user,
"pageIndex": pageIndex,
"totalPage": int(math.Ceil(float64(total) / float64(pageSize))),
"path": c.Request.URL.Path,
"maxReadPosts": models.MustListMaxReadPost(),
"maxCommentPosts": models.MustListMaxCommentPost(),
})
}
func AdminIndex(c *gin.Context) {
user, _ := c.Get(CONTEXT_USER_KEY)
c.HTML(http.StatusOK, "admin/index.html", gin.H{
"pageCount": models.CountPage(),
"postCount": models.CountPost(),
"tagCount": models.CountTag(),
"commentCount": models.CountComment(),
"user": user,
"comments": models.MustListUnreadComment(),
})
}