diff --git a/controllers/test/test.go b/controllers/test/test.go new file mode 100644 index 0000000..bfdfd1f --- /dev/null +++ b/controllers/test/test.go @@ -0,0 +1,10 @@ +package test + +import ( + "github.com/gin-gonic/gin" + "net/http" +) + +func Testjwt(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{"code": 20002, "msg": "处理请求成功"}) +} diff --git a/init/redisInit/redisInit.go b/init/redisInit/redisInit.go index 29bb199..44737ea 100644 --- a/init/redisInit/redisInit.go +++ b/init/redisInit/redisInit.go @@ -42,7 +42,7 @@ func RedisInit() error { // 测试连接 _, err = RedisClient.Ping(Ctx).Result() if err != nil { - return fmt.Errorf("Redis连接失败: %w", err) + panic(fmt.Errorf("Redis连接失败: %w", err)) } fmt.Println("Redis连接初始化成功") diff --git a/readme.md b/readme.md index 7258faa..112a27c 100644 --- a/readme.md +++ b/readme.md @@ -5,3 +5,4 @@ -10036 redis缓存失败 -20001 微信登录成功 +-20002 请求处理成功 diff --git a/router/setupRouter.go b/router/setupRouter.go index 1ad05c6..762f92a 100644 --- a/router/setupRouter.go +++ b/router/setupRouter.go @@ -2,8 +2,10 @@ package router import ( "github.com/gin-gonic/gin" + "toutoukan/controllers/test" "toutoukan/controllers/user" "toutoukan/socket" + "toutoukan/utill" ) func SetupRouter() *gin.Engine { @@ -12,6 +14,7 @@ func SetupRouter() *gin.Engine { apiGroup := r.Group("/user") { apiGroup.POST("/login", user.UserLogin) + apiGroup.POST("/test", utill.JWTAuthMiddleware(), test.Testjwt) } r.GET("/socket", func(c *gin.Context) { socket.WebsocketHandler(c) diff --git a/utill/jwtUt.go b/utill/jwtUt.go index d4574eb..0b05f56 100644 --- a/utill/jwtUt.go +++ b/utill/jwtUt.go @@ -84,6 +84,7 @@ func ExtractTokenFromHeader(c *gin.Context) string { // JWT+Redis验证中间件 func JWTAuthMiddleware() gin.HandlerFunc { + fmt.Println("验证请求") return func(c *gin.Context) { tokenString := ExtractTokenFromHeader(c) if tokenString == "" {