diff --git a/.gitignore b/.gitignore index 7cd1091..83aefd9 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,8 @@ # vendor/ # Go workspace file -go.work \ No newline at end of file +go.work + +.DS_Store + +.idea \ No newline at end of file diff --git a/config/application.yml b/config/application.yml index 1d8d2aa..ddc26a3 100644 --- a/config/application.yml +++ b/config/application.yml @@ -1,5 +1,5 @@ server: - port: 1016 + port: 8080 datasource: host: 127.0.0.1 port: 3306 diff --git a/main.go b/main.go index b926789..bf14e53 100644 --- a/main.go +++ b/main.go @@ -13,7 +13,7 @@ func init() { workDir, _ := os.Getwd() viper.SetConfigName("application") viper.SetConfigType("yml") - viper.AddConfigPath(workDir + "\\config") + viper.AddConfigPath(workDir + "/config") err := viper.ReadInConfig() if err != nil { panic("读取配置文件失败, error=" + err.Error()) @@ -27,7 +27,7 @@ func main() { r = router.CollectRoute(r) port := viper.GetString("server.port") if port != "" { - panic(r.Run("127.0.0.1:" + port)) + panic(r.Run(":" + port)) } - panic(r.Run("127.0.0.1:8080")) + panic(r.Run(":8080")) } diff --git a/middleware/authmiddleware.go b/middleware/authmiddleware.go index 03835bf..f3c088e 100644 --- a/middleware/authmiddleware.go +++ b/middleware/authmiddleware.go @@ -17,7 +17,7 @@ func AuthMiddleWare() gin.HandlerFunc { // 验证token if tokenString == "" || !strings.HasPrefix(tokenString, "Bearer ") { - c.JSON(http.StatusUnauthorized, gin.H{ + c.JSON(http.StatusOK, gin.H{ "code": http.StatusUnauthorized, "message": "请求未授权", }) @@ -29,7 +29,7 @@ func AuthMiddleWare() gin.HandlerFunc { token, claims, err := common.ParseToken(tokenString) if err != nil || !token.Valid { - c.JSON(http.StatusUnauthorized, gin.H{ + c.JSON(http.StatusOK, gin.H{ "code": http.StatusUnauthorized, "message": "请求未授权", }) @@ -44,7 +44,7 @@ func AuthMiddleWare() gin.HandlerFunc { // 用户不存在 if user.ID == 0 { - c.JSON(http.StatusUnauthorized, gin.H{ + c.JSON(http.StatusOK, gin.H{ "code": http.StatusUnauthorized, "message": "请求未授权", }) diff --git a/response/response.go b/response/response.go index 3d5bdcb..7dd1ab1 100644 --- a/response/response.go +++ b/response/response.go @@ -19,5 +19,5 @@ func Success(ctx *gin.Context, data gin.H, msg string) { } func Fail(ctx *gin.Context, data gin.H, msg string) { - Response(ctx, http.StatusBadRequest, 400, data, msg) + Response(ctx, http.StatusOK, 400, data, msg) } diff --git a/router/router.go b/router/router.go index 8191589..0184d11 100644 --- a/router/router.go +++ b/router/router.go @@ -7,8 +7,8 @@ import ( ) func CollectRoute(r *gin.Engine) *gin.Engine { - r.POST("/api/auth/register", controller.Register) - r.POST("/api/auth/login", controller.Login) - r.GET("/api/auth/info", middleware.AuthMiddleWare(), controller.Info) // 认证中间件保护info接口 + r.POST("/auth/register", controller.Register) + r.POST("/auth/login", controller.Login) + r.GET("/auth/info", middleware.AuthMiddleWare(), controller.Info) // 认证中间件保护info接口 return r }