Skip to content

Commit 921dc56

Browse files
committed
fix bind
1 parent 077652b commit 921dc56

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

pkg/gmc/handler/bot.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func init() {
5555

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

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

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

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

145145
func FetchQrCode(c *gin.Context) {
146146
req := &dto.FetchQRCodeReq{}
147-
err := c.Bind(req)
147+
err := Bind(c, req)
148148
if err != nil {
149149
c.String(http.StatusBadRequest, "bad request, not protobuf")
150150
return
@@ -175,7 +175,7 @@ func QueryQRCodeStatus(c *gin.Context) {
175175
queryQRCodeMutex.Lock()
176176
defer queryQRCodeMutex.Unlock()
177177
req := &dto.QueryQRCodeStatusReq{}
178-
err := c.Bind(req)
178+
err := Bind(c, req)
179179
if err != nil {
180180
c.String(http.StatusBadRequest, fmt.Sprintf("failed to bind, %+v", err))
181181
return
@@ -232,7 +232,7 @@ func QueryQRCodeStatus(c *gin.Context) {
232232

233233
func ListPlugin(c *gin.Context) {
234234
req := &dto.ListPluginReq{}
235-
err := c.Bind(req)
235+
err := Bind(c, req)
236236
if err != nil {
237237
c.String(http.StatusBadRequest, "bad request")
238238
return
@@ -268,7 +268,7 @@ func ListPlugin(c *gin.Context) {
268268

269269
func SavePlugin(c *gin.Context) {
270270
req := &dto.SavePluginReq{}
271-
err := c.Bind(req)
271+
err := Bind(c, req)
272272
if err != nil {
273273
c.String(http.StatusBadRequest, "bad request")
274274
return
@@ -311,7 +311,7 @@ func SavePlugin(c *gin.Context) {
311311

312312
func DeletePlugin(c *gin.Context) {
313313
req := &dto.DeletePluginReq{}
314-
err := c.Bind(req)
314+
err := Bind(c, req)
315315
if err != nil {
316316
c.String(http.StatusBadRequest, "bad request")
317317
return

pkg/gmc/handler/middlewares.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package handler
22

3-
import "github.com/gin-gonic/gin"
3+
import (
4+
"errors"
5+
"io/ioutil"
6+
7+
"github.com/gin-gonic/gin"
8+
"github.com/golang/protobuf/proto"
9+
)
410

511
func CORSMiddleware() gin.HandlerFunc {
612
return func(c *gin.Context) {
@@ -17,3 +23,18 @@ func CORSMiddleware() gin.HandlerFunc {
1723
c.Next()
1824
}
1925
}
26+
27+
func Bind(c *gin.Context, req any) error {
28+
buf, err := ioutil.ReadAll(c.Request.Body)
29+
if err != nil {
30+
return err
31+
}
32+
if r, ok := req.(proto.Message); ok {
33+
if err := proto.Unmarshal(buf, r); err != nil {
34+
return err
35+
}
36+
} else {
37+
return errors.New("obj is not ProtoMessage")
38+
}
39+
return nil
40+
}

0 commit comments

Comments
 (0)