mirror of
https://github.com/lzh-1625/go_process_manager.git
synced 2025-10-05 07:56:50 +08:00
update
This commit is contained in:
8
internal/app/constants/bind.go
Normal file
8
internal/app/constants/bind.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package constants
|
||||
|
||||
const (
|
||||
BIND_NONE = 0
|
||||
BIND_OPTION_HEADER = 1 << iota
|
||||
BIND_OPTION_BODY
|
||||
BIND_OPTION_QUERY
|
||||
)
|
@@ -142,3 +142,39 @@ func routePathInit(r *gin.Engine) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func bind[T any](fn func(*gin.Context, *T) error, bindOption int) func(*gin.Context) {
|
||||
return func(ctx *gin.Context) {
|
||||
var req *T
|
||||
if bindOption&constants.BIND_OPTION_BODY != 0 {
|
||||
if err := ctx.BindJSON(req); err != nil {
|
||||
rErr(ctx, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if bindOption&constants.BIND_OPTION_HEADER != 0 {
|
||||
if err := ctx.BindHeader(req); err != nil {
|
||||
rErr(ctx, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if bindOption&constants.BIND_OPTION_QUERY != 0 {
|
||||
if err := ctx.BindQuery(req); err != nil {
|
||||
rErr(ctx, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
err := fn(ctx, req)
|
||||
if err != nil {
|
||||
rErr(ctx, err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func rErr(ctx *gin.Context, err error) {
|
||||
ctx.JSON(500, gin.H{
|
||||
"code": -1,
|
||||
"message": err,
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user