This commit is contained in:
Max 2021-09-14 16:12:08 +08:00
parent ae4f89cb19
commit d5a9f7a54d
5 changed files with 78 additions and 8 deletions

View file

@ -4,7 +4,6 @@ import (
"github.com/spf13/cobra"
"github.com/yaoapp/gou"
"github.com/yaoapp/kun/utils"
"github.com/yaoapp/xiang/global"
"github.com/yaoapp/xiang/server"
)
@ -19,12 +18,6 @@ var startCmd = &cobra.Command{
utils.Dump(api.Name + ":" + p.Path)
}
}
gou.ServeHTTP(gou.Server{
Host: global.Conf.Service.Host,
Port: global.Conf.Service.Port,
Allows: global.Conf.Service.Allow,
Root: "/api",
}, server.Middlewares...)
server.Start()
},
}

1
go.mod
View file

@ -4,6 +4,7 @@ go 1.16
require (
github.com/caarlos0/env/v6 v6.7.1 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/elazarl/go-bindata-assetfs v1.0.1 // indirect
github.com/gin-gonic/gin v1.7.4 // indirect
github.com/joho/godotenv v1.3.0 // indirect

2
go.sum
View file

@ -69,6 +69,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/elazarl/go-bindata-assetfs v1.0.1 h1:m0kkaHRKEu7tUIUFVwhGGGYClXvyl4RE03qmvRTNfbw=
github.com/elazarl/go-bindata-assetfs v1.0.1/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=

57
server/guard.go Normal file
View file

@ -0,0 +1,57 @@
package server
import (
"fmt"
"strings"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"github.com/yaoapp/xiang/global"
)
// Guards 服务中间件
var Guards = map[string]gin.HandlerFunc{
"bearer-jwt": bearerJWT, // JWT 权限校验
}
// JwtClaims JWT claims
type JwtClaims struct {
ID int
Type string
Mobile string
Name string
jwt.StandardClaims
}
func bearerJWT(c *gin.Context) {
tokenString := c.Request.Header.Get("Authorization")
if tokenString == "" {
c.JSON(403, gin.H{"code": 403, "message": "无权访问该页面"})
c.Abort()
return
}
tokenString = strings.TrimSpace(strings.TrimPrefix(tokenString, "Bearer "))
token, err := jwt.ParseWithClaims(tokenString, &JwtClaims{}, func(token *jwt.Token) (interface{}, error) {
return global.Conf.JWT.Secret, nil
})
if err != nil {
c.JSON(403, gin.H{"code": 403, "message": fmt.Sprintf("登录已过期或令牌失效(%s)", err)})
c.Abort()
return
}
if claims, ok := token.Claims.(*JwtClaims); ok && token.Valid {
c.Set("id", claims.Subject)
c.Set("type", claims.Type)
c.Set("name", claims.Name)
c.Set("mobile", claims.Mobile)
c.Next()
return
}
// fmt.Println("bearer-JWT", token.Claims.Valid())
c.JSON(403, gin.H{"code": 403, "message": "无权访问该页面"})
c.Abort()
}

17
server/service.go Normal file
View file

@ -0,0 +1,17 @@
package server
import (
"github.com/yaoapp/gou"
"github.com/yaoapp/xiang/global"
)
// Start 启动服务
func Start() {
gou.SetHTTPGuards(Guards)
gou.ServeHTTP(gou.Server{
Host: global.Conf.Service.Host,
Port: global.Conf.Service.Port,
Allows: global.Conf.Service.Allow,
Root: "/api",
}, Middlewares...)
}