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.
paopao-ce/internal/servants/core/core.go

74 lines
1.5 KiB

package core
2 years ago
import (
"net/http"
"os"
"github.com/alimy/mir/v3"
2 years ago
"github.com/gin-gonic/gin"
"github.com/rocboss/paopao-ce/internal/model"
"github.com/rocboss/paopao-ce/pkg/xerror"
2 years ago
)
type BaseServant struct {
2 years ago
// TODO
}
type BaseBinding struct {
2 years ago
// TODO
}
type BaseRender struct {
2 years ago
// TODO
}
func (BaseServant) userFrom(c *gin.Context) (*model.User, bool) {
2 years ago
if u, exists := c.Get("USER"); exists {
user, ok := u.(*model.User)
return user, ok
}
return nil, false
}
func BindAny(c *gin.Context, obj any) mir.Error {
var errs xerror.ValidErrors
err := c.ShouldBind(obj)
if err != nil {
return mir.NewError(xerror.InvalidParams.Code(), xerror.InvalidParams.WithDetails(errs.Error()))
}
return nil
}
func RenderAny(c *gin.Context, data any, err mir.Error) {
if err == nil {
hostname, _ := os.Hostname()
if data == nil {
data = gin.H{
"code": 0,
"msg": "success",
"tracehost": hostname,
}
} else {
data = gin.H{
"code": 0,
"msg": "success",
"data": data,
"tracehost": hostname,
}
}
c.JSON(http.StatusOK, data)
} else {
// TODO: error process logic
resp := gin.H{"code": err.StatusCode(), "msg": err.Error()}
// xerr := &xerror.Error{}
// if errors.As(err, xerr) {
// resp["code"], resp["msg"] = xerr.Code(), xerr.Msg()
// details := xerr.Details()
// if len(details) > 0 {
// resp["details"] = details
// }
// }
c.JSON(http.StatusInternalServerError, resp)
}
}