-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
100 lines (85 loc) · 3.61 KB
/
api.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
package routes
import (
controllers "GoHub-Service/app/http/controllers/api/v1"
"GoHub-Service/app/http/controllers/api/v1/auth"
"GoHub-Service/app/http/middlewares"
"GoHub-Service/pkg/config"
"github.com/gin-gonic/gin"
)
// RegisterAPIRoutes 注册 API 相关路由
func RegisterAPIRoutes(r *gin.Engine) {
// 测试一个 v1 的路由组,我们所有的 v1 版本的路由都将存放到这里
var v1 *gin.RouterGroup
if len(config.Get("app.api_domain")) == 0 {
v1 = r.Group("/api/v1")
} else {
v1 = r.Group("/v1")
}
// 全局限流中间件:每小时限流。这里是所有 API (根据 IP)请求加起来。
// 作为参考 Github API 每小时最多 60 个请求(根据 IP)。
// 测试时,可以调高一点。
v1.Use(middlewares.LimitIP("200-H"))
{
authGroup := v1.Group("/auth")
// 限流中间件:每小时限流,作为参考 Github API 每小时最多 60 个请求(根据 IP)
// 测试时,可以调高一点
authGroup.Use(middlewares.LimitIP("1000-H"))
{
// 登录
lgc := new(auth.LoginController)
authGroup.POST("/login/using-phone", middlewares.GuestJWT(), lgc.LoginByPhone)
authGroup.POST("/login/using-password", middlewares.GuestJWT(), lgc.LoginByPassword)
authGroup.POST("/login/refresh-token", middlewares.AuthJWT(), lgc.RefreshToken)
// 重置密码
pwc := new(auth.PasswordController)
authGroup.POST("/password-reset/using-email", middlewares.GuestJWT(), pwc.ResetByEmail)
authGroup.POST("/password-reset/using-phone", middlewares.GuestJWT(), pwc.ResetByPhone)
// 注册用户
suc := new(auth.SignupController)
authGroup.POST("/signup/using-phone", middlewares.GuestJWT(), suc.SignupUsingPhone)
authGroup.POST("/signup/using-email", middlewares.GuestJWT(), suc.SignupUsingEmail)
authGroup.POST("/signup/phone/exist", middlewares.GuestJWT(), middlewares.LimitPerRoute("60-H"), suc.IsPhoneExist)
authGroup.POST("/signup/email/exist", middlewares.GuestJWT(), middlewares.LimitPerRoute("60-H"), suc.IsEmailExist)
// 发送验证码
vcc := new(auth.VerifyCodeController)
authGroup.POST("/verify-codes/phone", middlewares.LimitPerRoute("20-H"), vcc.SendUsingPhone)
authGroup.POST("/verify-codes/email", middlewares.LimitPerRoute("20-H"), vcc.SendUsingEmail)
// 图片验证码
authGroup.POST("/verify-codes/captcha", middlewares.LimitPerRoute("50-H"), vcc.ShowCaptcha)
}
uc := new(controllers.UsersController)
// 获取当前用户
v1.GET("/user", middlewares.AuthJWT(), uc.CurrentUser)
usersGroup := v1.Group("/users")
{
usersGroup.GET("", uc.Index)
usersGroup.PUT("", middlewares.AuthJWT(), uc.UpdateProfile)
usersGroup.PUT("/email", middlewares.AuthJWT(), uc.UpdateEmail)
usersGroup.PUT("/phone", middlewares.AuthJWT(), uc.UpdatePhone)
usersGroup.PUT("/password", middlewares.AuthJWT(), uc.UpdatePassword)
usersGroup.PUT("/avatar", middlewares.AuthJWT(), uc.UpdateAvatar)
}
cgc := new(controllers.CategoriesController)
cgcGroup := v1.Group("/categories")
{
cgcGroup.GET("", cgc.Index)
cgcGroup.POST("", middlewares.AuthJWT(), cgc.Store)
cgcGroup.PUT("/:id", middlewares.AuthJWT(), cgc.Update)
cgcGroup.DELETE("/:id", middlewares.AuthJWT(), cgc.Delete)
}
tpc := new(controllers.TopicsController)
tpcGroup := v1.Group("/topics")
{
tpcGroup.GET("", tpc.Index)
tpcGroup.POST("", middlewares.AuthJWT(), tpc.Store)
tpcGroup.PUT("/:id", middlewares.AuthJWT(), tpc.Update)
tpcGroup.DELETE("/:id", middlewares.AuthJWT(), tpc.Delete)
tpcGroup.GET("/:id", tpc.Show)
}
lsc := new(controllers.LinksController)
linksGroup := v1.Group("/links")
{
linksGroup.GET("", lsc.Index)
}
}
}