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/pkg/app/app.go

68 lines
1.3 KiB

// Copyright 2022 ROC. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
3 years ago
package app
import (
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/rocboss/paopao-ce/pkg/xerror"
3 years ago
)
type Response struct {
Ctx *gin.Context
}
type Pager struct {
Page int `json:"page"`
PageSize int `json:"page_size"`
TotalRows int64 `json:"total_rows"`
}
func NewResponse(ctx *gin.Context) *Response {
return &Response{Ctx: ctx}
}
func (r *Response) ToResponse(data any) {
3 years ago
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,
}
}
r.Ctx.JSON(http.StatusOK, data)
}
func (r *Response) ToResponseList(list any, totalRows int64) {
3 years ago
r.ToResponse(gin.H{
"list": list,
"pager": Pager{
Page: GetPage(r.Ctx),
PageSize: GetPageSize(r.Ctx),
TotalRows: totalRows,
},
})
}
func (r *Response) ToErrorResponse(err *xerror.Error) {
response := gin.H{"code": err.StatusCode(), "msg": err.Msg()}
3 years ago
details := err.Details()
if len(details) > 0 {
response["details"] = details
}
r.Ctx.JSON(xerror.HttpStatusCode(err), response)
3 years ago
}