master
zggsong 2 years ago
parent ca9eb1a124
commit add9c45a4f

4
.gitignore vendored

@ -19,3 +19,7 @@
# Go workspace file # Go workspace file
go.work go.work
.DS_Store
.idea

@ -1,5 +1,5 @@
server: server:
port: 1016 port: 8080
datasource: datasource:
host: 127.0.0.1 host: 127.0.0.1
port: 3306 port: 3306

@ -13,7 +13,7 @@ func init() {
workDir, _ := os.Getwd() workDir, _ := os.Getwd()
viper.SetConfigName("application") viper.SetConfigName("application")
viper.SetConfigType("yml") viper.SetConfigType("yml")
viper.AddConfigPath(workDir + "\\config") viper.AddConfigPath(workDir + "/config")
err := viper.ReadInConfig() err := viper.ReadInConfig()
if err != nil { if err != nil {
panic("读取配置文件失败, error=" + err.Error()) panic("读取配置文件失败, error=" + err.Error())
@ -27,7 +27,7 @@ func main() {
r = router.CollectRoute(r) r = router.CollectRoute(r)
port := viper.GetString("server.port") port := viper.GetString("server.port")
if 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"))
} }

@ -17,7 +17,7 @@ func AuthMiddleWare() gin.HandlerFunc {
// 验证token // 验证token
if tokenString == "" || !strings.HasPrefix(tokenString, "Bearer ") { if tokenString == "" || !strings.HasPrefix(tokenString, "Bearer ") {
c.JSON(http.StatusUnauthorized, gin.H{ c.JSON(http.StatusOK, gin.H{
"code": http.StatusUnauthorized, "code": http.StatusUnauthorized,
"message": "请求未授权", "message": "请求未授权",
}) })
@ -29,7 +29,7 @@ func AuthMiddleWare() gin.HandlerFunc {
token, claims, err := common.ParseToken(tokenString) token, claims, err := common.ParseToken(tokenString)
if err != nil || !token.Valid { if err != nil || !token.Valid {
c.JSON(http.StatusUnauthorized, gin.H{ c.JSON(http.StatusOK, gin.H{
"code": http.StatusUnauthorized, "code": http.StatusUnauthorized,
"message": "请求未授权", "message": "请求未授权",
}) })
@ -44,7 +44,7 @@ func AuthMiddleWare() gin.HandlerFunc {
// 用户不存在 // 用户不存在
if user.ID == 0 { if user.ID == 0 {
c.JSON(http.StatusUnauthorized, gin.H{ c.JSON(http.StatusOK, gin.H{
"code": http.StatusUnauthorized, "code": http.StatusUnauthorized,
"message": "请求未授权", "message": "请求未授权",
}) })

@ -19,5 +19,5 @@ func Success(ctx *gin.Context, data gin.H, msg string) {
} }
func Fail(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)
} }

@ -7,8 +7,8 @@ import (
) )
func CollectRoute(r *gin.Engine) *gin.Engine { func CollectRoute(r *gin.Engine) *gin.Engine {
r.POST("/api/auth/register", controller.Register) r.POST("/auth/register", controller.Register)
r.POST("/api/auth/login", controller.Login) r.POST("/auth/login", controller.Login)
r.GET("/api/auth/info", middleware.AuthMiddleWare(), controller.Info) // 认证中间件保护info接口 r.GET("/auth/info", middleware.AuthMiddleWare(), controller.Info) // 认证中间件保护info接口
return r return r
} }

Loading…
Cancel
Save