Skip to content

Commit

Permalink
Merge pull request #5 from afzw/dev
Browse files Browse the repository at this point in the history
feat: http request 参数获取优化
  • Loading branch information
xudaqian1 authored Feb 16, 2024
2 parents 5789b62 + dab3269 commit aa570fa
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 1,320 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@

基于属性的权限验证(ABAC,Attribute-Based Access Control)的微服务,为其他业务服务提供权限认证。


## 参考
https://github.com/marmotedu/iam/tree/v1.0.0
28 changes: 12 additions & 16 deletions api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,29 @@ package api
import (
"auth-server/middleware"
"auth-server/model"
"strconv"

"github.com/gin-gonic/gin"
"strconv"
)

type GetUserParam struct {
Id string `uri:"id" binding:"required"`
}

type UserApi struct{}

func (UserApi) CreateUser(c *gin.Context) {
var param model.User
if err := c.ShouldBindJSON(&param); err != nil {
middleware.Fail(c, model.ErrParam.AddErr(err))
return
}
err := userService.CreateUser(&param)
middleware.Auto(c, err, nil)
_, _, param := middleware.Validate[any, any, model.User](c)

errCreate := userService.CreateUser(&param)
middleware.Auto(c, errCreate, nil)
return
}

func (UserApi) GetUser(c *gin.Context) {
id := c.Param("id")
if id == "" {
middleware.Fail(c, model.ErrParam)
return
}
userId, err := strconv.ParseInt(id, 10, 64)
params, _, _ := middleware.Validate[GetUserParam, any, any](c)

userId, err := strconv.ParseInt(params.Id, 10, 64)
if err != nil {
middleware.Fail(c, model.ErrParam.AddErr(err))
return
Expand All @@ -38,9 +36,7 @@ func (UserApi) GetUser(c *gin.Context) {
}

func (UserApi) UpdateUser(gin *gin.Context) {

}

func (UserApi) DeleteUser(gin *gin.Context) {

}
46 changes: 39 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,52 @@ module auth-server
go 1.20

require (
github.com/bytedance/sonic v1.10.0 // indirect
github.com/gin-gonic/gin v1.9.1
github.com/go-playground/validator/v10 v10.15.1 // indirect
github.com/golang-jwt/jwt/v5 v5.2.0
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/mattn/go-sqlite3 v1.14.19 // indirect
github.com/pelletier/go-toml/v2 v2.0.9 // indirect
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.3
github.com/spf13/viper v1.16.0
gorm.io/driver/sqlite v1.5.4
gorm.io/gorm v1.25.5
)

require (
github.com/bytedance/sonic v1.10.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-sqlite3 v1.14.19 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.9 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.4.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gorm.io/driver/sqlite v1.5.4 // indirect
gorm.io/gorm v1.25.5 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
1,302 changes: 10 additions & 1,292 deletions go.sum

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions middleware/validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package middleware

import (
"auth-server/model"

"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)

func Validate[P, Q, B any](c *gin.Context) (params P, query Q, body B) {
if err := c.ShouldBindUri(&params); err != nil {
Fail(c, model.ErrParam.AddErr(err))
return
}

if err := c.ShouldBindWith(&query, binding.Query); err != nil {
Fail(c, model.ErrParam.AddErr(err))
return
}

if c.Request.ContentLength != 0 {
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
Fail(c, model.ErrParam.AddErr(err))
return
}
}

return
}
6 changes: 5 additions & 1 deletion model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type User struct {
BaseInfo
Username string `json:"username" gorm:"unique_index" binding:"required"`
Password string `json:"password" binding:"required"`
Email string `json:"email" gorm:"unique_index" binding:"required"`
Email string `json:"email" gorm:"unique_index" binding:"required,email"`
Avatar string `json:"avatar"`
Salt string `json:"salt"`
}
Expand All @@ -15,3 +15,7 @@ type UpdateUser struct {
Password string `json:"password"`
Avatar string `json:"avatar"`
}

type GetUserParams struct {
Id string `uri:"id" validate:"required"`
}
3 changes: 2 additions & 1 deletion router/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"auth-server/api"
"auth-server/config"
"auth-server/middleware"
"strconv"

"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"strconv"
)

var (
Expand Down
6 changes: 4 additions & 2 deletions router/user.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package router

import "github.com/gin-gonic/gin"
import (
"github.com/gin-gonic/gin"
)

func userRouter(router *gin.RouterGroup) {
//user
// user
router.GET("/users/:id", apiUser.GetUser)
router.POST("/users", apiUser.CreateUser)
router.PATCH("/users/:id", apiUser.UpdateUser)
Expand Down

0 comments on commit aa570fa

Please sign in to comment.