You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
package api
import ( "context" "encoding/json" "git.aiterp.net/lucifer/new-server/models" "github.com/gin-gonic/gin" "strconv" )
var errorMap = map[error]int{ models.ErrNotFound: 404, models.ErrInvalidName: 400, models.ErrBadInput: 400, models.ErrBadColor: 400, models.ErrInternal: 500, models.ErrUnknownColorFormat: 400, }
type response struct { Code int `json:"code"` Message string `json:"message"` Data interface{} `json:"data"` }
func handler(fun func(c *gin.Context) (interface{}, error)) gin.HandlerFunc { return func(c *gin.Context) { val, err := fun(c) if err != nil { errCode := errorMap[err] if errCode == 0 { errCode = 500 }
c.JSON(errCode, response{ Code: errCode, Message: err.Error(), }) return }
c.JSON(200, response{ Code: 200, Message: "success", Data: val, }) } }
func intParam(c *gin.Context, key string) int { i, err := strconv.Atoi(c.Param(key)) if err != nil { return 0 }
return i }
func parseBody(c *gin.Context, target interface{}) error { err := json.NewDecoder(c.Request.Body).Decode(target) if err != nil { return models.ErrBadInput }
return nil }
func ctxOf(c *gin.Context) context.Context { return c.Request.Context() }
|