-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
69 lines (56 loc) · 1.63 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
package main
import (
"HarpBlog/utils"
"html/template"
"strconv"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.Static("/static", "./static")
r.StaticFile("/favicon.ico", "./static/favicon.ico")
r.LoadHTMLFiles("templates/article.html", "templates/home.html")
r.GET("/", func(c *gin.Context) {
pageS := c.Query("page")
page := 1
if pageS != "" {
page, _ = strconv.Atoi(pageS)
}
c.HTML(200, "home.html", gin.H{
"articles": utils.GetArticles(page),
"nav": utils.GetHTMLComponent("nav.html"),
"calendar": utils.GetHTMLComponent("calendar.html"),
"pagination": utils.GeneratePagination(page),
})
})
r.GET("/articles/:articleId", func(c *gin.Context) {
articleId, err := strconv.ParseUint(c.Param("articleId"), 10, 16)
if err != nil {
panic(err)
}
c.HTML(200, "article.html", gin.H{
"content": template.HTML(utils.GetArticleContent(articleId)),
"info": utils.GetArticleMeta(articleId),
"nav": utils.GetHTMLComponent("nav.html"),
"calendar": utils.GetHTMLComponent("calendar.html"),
})
})
r.GET("/search", func(c *gin.Context) {
keyword := c.Query("keyword")
if keyword != "" {
c.HTML(200, "home.html", gin.H{
"articles": utils.SearchArticles(keyword),
"nav": utils.GetHTMLComponent("nav.html"),
"calendar": utils.GetHTMLComponent("calendar.html"),
})
} else {
c.Redirect(302, "/")
}
})
r.NoRoute(func(c *gin.Context) {
c.SetCookie("password", "*7123Gsd#123", 3600, "/", "localhost", false, true)
c.Header("Content-Type", "text/html; charset=utf-8")
c.String(404, "<h3>404 Not Found</h3>")
})
_ = r.Run(":6969")
}