init repo
This commit is contained in:
38
common/helper.go
Normal file
38
common/helper.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/nose7en/ToyBoomServer/defs"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func GlobalClientID(username, clientType, clientID string) string {
|
||||
return fmt.Sprintf("%s.%s.%s", username, clientType, clientID)
|
||||
}
|
||||
|
||||
func Wrapper[T ReqType, U RespType](handler func(context.Context, *T) (*U, error)) func(c *gin.Context) {
|
||||
return func(c *gin.Context) {
|
||||
Logger(c).Infof("request url: %s", c.Request.URL)
|
||||
req, err := GetProtoRequest[T](c)
|
||||
if err != nil {
|
||||
Logger(c).WithError(err).Errorf("Failed to get request, url: %s", c.Request.URL)
|
||||
ErrResp(c, &defs.CommonResponse{
|
||||
Status: &defs.Status{Code: defs.RespCode_INVALID, Message: defs.RespMessage_INVALID},
|
||||
}, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := handler(c, req)
|
||||
if err != nil {
|
||||
Logger(c).WithError(err).Errorf("Failed to handle request, url: %s", c.Request.URL)
|
||||
ErrResp(c, resp, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
OKResp(c, resp)
|
||||
Logger(c).Infof("handle request success, response url: %s", c.Request.URL)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user