diff --git a/internal/app/constants/bind.go b/internal/app/constants/bind.go new file mode 100644 index 0000000..a58e0a2 --- /dev/null +++ b/internal/app/constants/bind.go @@ -0,0 +1,8 @@ +package constants + +const ( + BIND_NONE = 0 + BIND_OPTION_HEADER = 1 << iota + BIND_OPTION_BODY + BIND_OPTION_QUERY +) diff --git a/internal/app/route/route.go b/internal/app/route/route.go index 5172a99..c3cdefa 100644 --- a/internal/app/route/route.go +++ b/internal/app/route/route.go @@ -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, + }) +}