Skip to content

Commit

Permalink
fix bind
Browse files Browse the repository at this point in the history
  • Loading branch information
lz1998 committed Jun 22, 2022
1 parent 077652b commit 921dc56
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
18 changes: 9 additions & 9 deletions pkg/gmc/handler/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func init() {

func CreateBot(c *gin.Context) {
req := &dto.CreateBotReq{}
err := c.Bind(req)
err := Bind(c, req)
if err != nil {
c.String(http.StatusBadRequest, "bad request, not protobuf")
return
Expand All @@ -78,7 +78,7 @@ func CreateBot(c *gin.Context) {

func DeleteBot(c *gin.Context) {
req := &dto.DeleteBotReq{}
err := c.Bind(req)
err := Bind(c, req)
if err != nil {
c.String(http.StatusBadRequest, "bad request, not protobuf")
return
Expand All @@ -95,7 +95,7 @@ func DeleteBot(c *gin.Context) {

func ListBot(c *gin.Context) {
req := &dto.ListBotReq{}
err := c.Bind(req)
err := Bind(c, req)
if err != nil {
c.String(http.StatusBadRequest, "bad request, not protobuf")
return
Expand All @@ -121,7 +121,7 @@ func ListBot(c *gin.Context) {

func SolveCaptcha(c *gin.Context) {
req := &dto.SolveCaptchaReq{}
err := c.Bind(req)
err := Bind(c, req)
if err != nil {
c.String(http.StatusBadRequest, "bad request, not protobuf")
return
Expand All @@ -144,7 +144,7 @@ func SolveCaptcha(c *gin.Context) {

func FetchQrCode(c *gin.Context) {
req := &dto.FetchQRCodeReq{}
err := c.Bind(req)
err := Bind(c, req)
if err != nil {
c.String(http.StatusBadRequest, "bad request, not protobuf")
return
Expand Down Expand Up @@ -175,7 +175,7 @@ func QueryQRCodeStatus(c *gin.Context) {
queryQRCodeMutex.Lock()
defer queryQRCodeMutex.Unlock()
req := &dto.QueryQRCodeStatusReq{}
err := c.Bind(req)
err := Bind(c, req)
if err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("failed to bind, %+v", err))
return
Expand Down Expand Up @@ -232,7 +232,7 @@ func QueryQRCodeStatus(c *gin.Context) {

func ListPlugin(c *gin.Context) {
req := &dto.ListPluginReq{}
err := c.Bind(req)
err := Bind(c, req)
if err != nil {
c.String(http.StatusBadRequest, "bad request")
return
Expand Down Expand Up @@ -268,7 +268,7 @@ func ListPlugin(c *gin.Context) {

func SavePlugin(c *gin.Context) {
req := &dto.SavePluginReq{}
err := c.Bind(req)
err := Bind(c, req)
if err != nil {
c.String(http.StatusBadRequest, "bad request")
return
Expand Down Expand Up @@ -311,7 +311,7 @@ func SavePlugin(c *gin.Context) {

func DeletePlugin(c *gin.Context) {
req := &dto.DeletePluginReq{}
err := c.Bind(req)
err := Bind(c, req)
if err != nil {
c.String(http.StatusBadRequest, "bad request")
return
Expand Down
23 changes: 22 additions & 1 deletion pkg/gmc/handler/middlewares.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package handler

import "github.com/gin-gonic/gin"
import (
"errors"
"io/ioutil"

"github.com/gin-gonic/gin"
"github.com/golang/protobuf/proto"
)

func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
Expand All @@ -17,3 +23,18 @@ func CORSMiddleware() gin.HandlerFunc {
c.Next()
}
}

func Bind(c *gin.Context, req any) error {
buf, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
return err
}
if r, ok := req.(proto.Message); ok {
if err := proto.Unmarshal(buf, r); err != nil {
return err
}
} else {
return errors.New("obj is not ProtoMessage")
}
return nil
}

0 comments on commit 921dc56

Please sign in to comment.