1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- package main
- import (
- "net/http"
- "server/msg"
- "github.com/gin-contrib/cors"
- "github.com/gin-gonic/gin"
- "github.com/gin-gonic/gin/binding"
- "google.golang.org/protobuf/proto"
- )
- func ParseProtoBuf(message proto.Message) gin.HandlerFunc {
- return func(c *gin.Context) {
-
- msg := proto.Clone(message)
-
- if err := c.ShouldBindBodyWith(msg, binding.ProtoBuf); err != nil {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
- "error": "Invalid protobuf data",
- "details": err.Error(),
- })
- return
- }
-
- c.Set("protobuf_data", msg)
- c.Next()
- }
- }
- func main() {
- r := gin.Default()
-
- config := cors.DefaultConfig()
- config.AllowOrigins = []string{"*"}
- config.AllowMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"}
- config.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Cookie"}
- config.AllowCredentials = true
- r.Use(cors.New(config))
-
-
-
-
-
-
-
-
-
- r.POST("/ReqLogin", ParseProtoBuf(&msg.ReqLogin{}), ReqLogin)
- r.POST("/ReqRegister", ParseProtoBuf(&msg.ReqRegister{}), ReqRegister)
- r.POST("/ReqShop", ParseProtoBuf(&msg.ReqShop{}), ReqShop)
-
- r.POST("/GetHistoryList", ParseProtoBuf(&msg.ReqLudoHistory{}), GetHistorList)
- r.POST("/GetLudoRoomInfo", ParseProtoBuf(&msg.ReqLudoRoomInfo{}), GetLudoRoomInfo)
-
-
-
-
- r.Run(":8080")
- }
|