-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
120 lines (101 loc) · 3.04 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
package main
import (
"github.com/Curricane/logger"
"github.com/gin-gonic/gin"
"qasystem/controller/account"
"qasystem/controller/answer"
"qasystem/controller/category"
"qasystem/controller/comment"
"qasystem/controller/favorite"
"qasystem/controller/question"
"qasystem/filter"
mdlacc "qasystem/middleware/account"
"qasystem/dal/db"
"qasystem/id_gen"
)
func initDb() (err error) {
dns := "root:cmc123456@tcp(localhost:3306)/qasystem?parseTime=true"
err = db.Init(dns)
if err != nil {
return
}
return
}
func initSession() (err error) {
err = mdlacc.InitSession("memory", "")
return
}
func initFilter() (err error) {
err = filter.Init("./data/filter.dat.txt")
if err != nil {
logger.Error("init filter failed, err:%v", err)
return
}
logger.Debug("init filter succ")
return
}
func initTemplate(router *gin.Engine) {
// 路径映射,访问/ 实际访问/static/index.html
router.StaticFile("/", "./static/index.html")
//
router.StaticFile("/favicon.ico", "./static/favicon.ico")
// css静态资源路径映射
router.Static("/css/", "./static/css/")
// fonts静态资源路径映射
router.Static("/fonts/", "./static/fonts/")
// img静态资源路径映射
router.Static("/img/", "./static/img/")
// js静态资源路径映射
router.Static("/js/", "./static/js/")
}
func main() {
router := gin.Default()
// 初始化日志模块
config := make(map[string]string)
config["log_level"] = "debug"
logger.InitLogger("console", config)
// 初始化DB模块
err := initDb()
if err != nil {
panic(err)
}
// 初始化会话模块
err = initSession()
if err != nil {
panic(err)
}
// 初始化 id_gen,值后续需要更为实际的机器序列
err = id_gen.Init(1)
if err != nil {
panic(err)
}
err = initFilter()
if err != nil {
panic(err)
}
// 初始化前端资源
initTemplate(router)
// 设置路由
router.POST("/api/user/register", account.RegisterHandle)
router.POST("/api/user/login", account.LoginHandle)
router.GET("/api/category/list", category.GetCategoryListHandle)
router.POST("/api/ask/submit", mdlacc.AuthMiddleware, question.QuestionSubmitHandle)
router.GET("/api/question/list", category.GetQuestionListHandle)
router.GET("/api/question/detail", question.QuestionDetailHandle)
router.GET("/api/answer/list", answer.AnswerListHandle)
router.POST("/api/answer/commit", mdlacc.AuthMiddleware, answer.AnswerCommitHandle)
// 评论模块
commentGroup := router.Group("/api/comment/", mdlacc.AuthMiddleware)
commentGroup.POST("/post_comment", comment.PostCommentHandle)
commentGroup.POST("/post_reply", comment.PostReplyHandle)
commentGroup.GET("/list", comment.CommentListHandle)
commentGroup.GET("/reply_list", comment.ReplyListHandle)
commentGroup.POST("/like", comment.LikeHandle)
//收藏模块路由
favoriteGroup := router.Group("/api/favorite/")
favoriteGroup.POST("/add_dir", favorite.AddDirHandle)
favoriteGroup.POST("/add", favorite.AddFavoriteHandle)
favoriteGroup.GET("/dir_list", favorite.DirListHandle)
favoriteGroup.GET("/list", favorite.FavoriteListHandle)
router.Run(":9090")
}