-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
291 lines (262 loc) · 8.05 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
package main
import (
"myoo/jwt"
"myoo/models"
"myoo/plugins"
"myoo/redis"
"myoo/router/v1/account"
"myoo/router/v1/admin"
"myoo/router/v1/auth"
"myoo/router/v1/author"
"myoo/router/v1/category"
"myoo/router/v1/checkcode"
"myoo/router/v1/comments"
"myoo/router/v1/download"
"myoo/router/v1/followers"
"myoo/router/v1/inc"
"myoo/router/v1/index"
"myoo/router/v1/menus"
"myoo/router/v1/posts"
"myoo/router/v1/recommend"
"myoo/router/v1/sign"
"github.com/gin-gonic/gin"
)
func init() {
conf := plugins.NewConfig()
jwt.NewJwtKey(conf.GetKey())
plugins.NewAesKey(conf.GetKey())
models.RegisterDB(conf.GetDbAddr())
redis.New(conf.GetRedisAddr(), conf.GetRedisPass())
}
func main() {
/*******************Gin初始化*******************/
r := gin.New()
r.Use(FilterXsrf(), FilterJwt())
/*******************v1路由组*******************/
rest := r.Group("/rest/v1")
{
// RouterCheckCode(rest) //验证码
RouterAuth(rest)
RouterInc(rest) //信息
RouterMenus(rest) //菜单
RouterIndex(rest) //首页
RouterFollowers(rest) //关注事件
RouterCategory(rest) //分类
RouterAuthor(rest) //用户空间
RouterSign(rest) //登录
RouterAccount(rest) //个人中心
RouterPosts(rest) //文章
RouterRecommend(rest) //推荐文章
RouterDownload(rest) //下载
RouterComments(rest) //评论
RouterAdmin(rest) //管理页面
}
/*******************模板输出*******************/
if plugins.GetConfig().GetServerRender() {
r.LoadHTMLGlob("views/*")
r.NoRoute(RouterViews) //未知路由处理
}
/*******************静态文件夹*******************/
r.Static("/src", "src")
r.Run(plugins.GetConfig().GetPort())
}
/**************************************
路由事件
**************************************/
/*******************验证码*******************/
func RouterCheckCode(rest *gin.RouterGroup) {
rest.Use(FilterCheckCode())
rest.GET("/checkcode", new(checkcode.CheckCode).Get)
return
}
/*******************auth*******************/
func RouterAuth(rest *gin.RouterGroup) {
rest.GET("/auth", new(auth.Auth).Get)
return
}
/*******************信息*******************/
func RouterInc(rest *gin.RouterGroup) {
rest.GET("/inc", new(inc.Inc).Get)
return
}
/*******************菜单*******************/
func RouterMenus(rest *gin.RouterGroup) {
rest.GET("/menus", new(menus.Menus).Get)
return
}
/*******************首页*******************/
func RouterIndex(rest *gin.RouterGroup) {
rest.GET("/index", new(index.Index).Get)
return
}
/*******************关注*******************/
func RouterFollowers(rest *gin.RouterGroup) {
rest.POST("/followers", new(followers.Followers).Post)
return
}
/*******************分类*******************/
func RouterCategory(rest *gin.RouterGroup) {
rest.GET("/category/:id/:page", new(category.Category).Get)
return
}
/*******************推荐文章*******************/
func RouterRecommend(rest *gin.RouterGroup) {
rest.GET("/recommend/:id", new(recommend.Recommend).Get)
return
}
/*******************空间*******************/
func RouterAuthor(rest *gin.RouterGroup) {
router := rest.Group("/author")
{
router.GET("/:id", new(author.Author).Get)
router.GET("/:id/posts/:page", new(author.Posts).Get)
router.GET("/:id/followers/:page", new(author.Followers).Get)
router.GET("/:id/fans/:page", new(author.Fans).Get)
}
return
}
/*******************登录*******************/
func RouterSign(rest *gin.RouterGroup) {
router := rest.Group("/sign")
{
router.POST("/login", new(sign.Login).Post)
router.GET("/registered", new(sign.Registered).Get)
router.POST("/registered", new(sign.Registered).Post)
router.POST("/forgetpwd", new(sign.Forgetpwd).Post)
router.POST("/check/:path", new(sign.ResetPwd).Post)
}
return
}
/*******************文章*******************/
func RouterPosts(rest *gin.RouterGroup) {
router := rest.Group("/posts")
{
router.GET("/:id", new(posts.Posts).Get)
router.POST("/:id", new(posts.Posts).Post)
}
return
}
/*******************下载*******************/
func RouterDownload(rest *gin.RouterGroup) {
router := rest.Group("/download")
{
router.GET("/:path", new(download.Download).Get)
}
return
}
/*******************评论*******************/
func RouterComments(rest *gin.RouterGroup) {
router := rest.Group("/comments")
{
router.GET("/:id/:page", new(comments.Comments).Get)
router.POST("/:id", new(comments.Comments).Post)
}
return
}
/*******************个人中心*******************/
func RouterAccount(rest *gin.RouterGroup) {
router := rest.Group("/account")
router.Use(FilterAuth())
{
router.GET("", new(account.Account).Get)
router.POST("/info", new(account.Info).Post)
router.GET("/newposts", new(account.NewPosts).Get)
router.POST("/newposts", new(account.NewPosts).Post)
router.GET("/posts/:page", new(account.Posts).Get)
router.GET("/favorite/:page", new(account.Favorite).Get)
router.GET("/comments/:page", new(account.Comments).Get)
}
return
}
/*******************管理*******************/
func RouterAdmin(rest *gin.RouterGroup) {
router := rest.Group("/admin")
router.Use(FilterAdmin())
{
router.GET("", new(admin.Admin).Get)
router.GET("/statistics", new(admin.Statistics).Get)
router.GET("/options", new(admin.Options).Get)
router.POST("/options", new(admin.Options).Post)
router.GET("/category", new(admin.Category).Get)
router.POST("/category", new(admin.Category).Post)
router.GET("/menus", new(admin.Menus).Get)
router.POST("/menus", new(admin.Menus).Post)
router.GET("/box", new(admin.Box).Get)
router.POST("/box", new(admin.Box).Post)
router.GET("/posts/:page", new(admin.Posts).Get)
router.GET("/user/:id", new(admin.User).Get)
router.POST("/user/:id", new(admin.User).Post)
router.GET("/users/:page", new(admin.Users).Get)
router.GET("/group", new(admin.Group).Get)
router.POST("/group", new(admin.Group).Post)
router.GET("/modifyposts/:id", new(admin.ModifyPosts).Get)
router.POST("/modifyposts/:id", new(admin.ModifyPosts).Post)
router.POST("/cache", new(admin.Cache).Post)
}
return
}
/*******************模板*******************/
func RouterViews(rest *gin.Context) {
rest.HTML(200, "index.html", gin.H{})
return
}
/**************************************
中间件
**************************************/
/*******************Xssrf处理*******************/
func FilterXsrf() gin.HandlerFunc {
return func(router *gin.Context) {
router.Header("Access-Control-Allow-Headers", "*")
router.Header("Access-Control-Allow-Origin", "*")
router.Next()
}
}
/*******************验证码处理*******************/
func FilterCheckCode() gin.HandlerFunc {
return func(router *gin.Context) {
code := router.PostForm("checkcode")
key := router.PostForm("checkkey")
if len(code) != 6 || !checkcode.VerfiyCaptcha(key, code) {
router.JSON(200, gin.H{"success": 403, "message": "验证码错误", "data": nil})
router.Abort()
return
}
router.Next()
return
}
}
/*******************JWT处理*******************/
func FilterJwt() gin.HandlerFunc {
return func(router *gin.Context) {
if id, ok := jwt.Decode(router.GetHeader("X-MYOO")); ok {
router.Set("auth", id)
} else {
router.Set("auth", nil)
}
router.Next()
}
}
/*******************限制登录*******************/
func FilterAuth() gin.HandlerFunc {
return func(router *gin.Context) {
if id := router.GetInt64("auth"); id == 0 {
router.JSON(403, gin.H{"success": 403, "message": "请先登录", "data": nil})
router.Abort()
return
}
router.Next()
}
}
/*******************是否管理员*******************/
func FilterAdmin() gin.HandlerFunc {
return func(router *gin.Context) {
id := router.GetInt64("auth")
if auth := redis.Query_User(id); auth.Admin {
router.Next()
return
}
router.JSON(403, gin.H{"success": 403, "message": "请先登录", "data": nil})
router.Abort()
return
}
}