package common import ( "bytes" "encoding/json" "io/ioutil" "time" "github.com/bwmarrin/snowflake" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" ) type M = map[string]interface{} func OrString(ss ...string) string { for _, s := range ss { if s != "" { return s } } return "" } func GenGid() string { return gNode.Generate().Base58() } var gNode *snowflake.Node = nil func init() { node, err := snowflake.NewNode(1) if err != nil { panic(err) } gNode = node } func PanicIfError(err error) { if err != nil { panic(err) } } func If(condition bool, trueObj interface{}, falseObj interface{}) interface{} { if condition { return trueObj } return falseObj } func MustMarshal(v interface{}) []byte { b, err := json.Marshal(v) PanicIfError(err) return b } func MustMarshalString(v interface{}) string { return string(MustMarshal(v)) } func MustUnmarshal(b []byte, obj interface{}) { err := json.Unmarshal(b, obj) PanicIfError(err) } func MustUnmarshalString(s string, obj interface{}) { MustUnmarshal([]byte(s), obj) } func MustRemarshal(from interface{}, to interface{}) { b, err := json.Marshal(from) PanicIfError(err) err = json.Unmarshal(b, to) PanicIfError(err) } func GetGinApp() *gin.Engine { gin.SetMode(gin.ReleaseMode) app := gin.Default() app.Use(func(c *gin.Context) { body := "" if c.Request.Method == "POST" { rb, err := c.GetRawData() if err != nil { logrus.Printf("GetRawData error: %s", err.Error()) } else { body = string(rb) c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(rb)) } } began := time.Now() logrus.Printf("begin %s %s query: %s body: %s", c.Request.Method, c.FullPath(), c.Request.URL.RawQuery, body) c.Next() logrus.Printf("used %d ms %s %s query: %s body: %s", time.Since(began).Milliseconds(), c.Request.Method, c.FullPath(), c.Request.URL.RawQuery, body) }) return app } func WrapHandler(fn func(*gin.Context) (interface{}, error)) gin.HandlerFunc { return func(c *gin.Context) { r, err := fn(c) var b = []byte{} if err == nil { b, err = json.Marshal(r) } if err != nil { logrus.Printf("status: 500, code: 500 message: %s", err.Error()) c.JSON(500, M{"code": 500, "message": err.Error()}) } else { logrus.Printf("status: 200, content: %s", string(b)) c.Status(200) c.Writer.Header().Add("Content-Type", "application/json") c.Writer.Write(b) } } }