Ai
1 Star 0 Fork 0

lipore/plume

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
rest.go 833 Bytes
一键复制 编辑 原始数据 按行查看 历史
lipore 提交于 2023-11-01 19:15 +08:00 . feat: add json response
package v2
import (
"net/http"
"github.com/gin-gonic/gin"
)
const (
Success = "success"
Error = "error"
Status = "status"
)
func Response(c *gin.Context, obj any) {
c.Header(Status, string(Success))
c.JSON(http.StatusOK, obj)
}
type CRUDApi interface {
Get(c *gin.Context)
List(c *gin.Context)
Create(c *gin.Context)
Update(c *gin.Context)
Delete(c *gin.Context)
}
func RegisterCRUDApi(route *gin.Engine, pathPrefix string, api CRUDApi, middlewares ...gin.HandlerFunc) {
route.
Group(pathPrefix, middlewares...).
GET("/", api.List).
POST("/", api.Create).
GET("/:id", api.Get).
PUT("/:id", api.Update).
DELETE("/:id", api.Delete)
}
func JsonResponse(c *gin.Context, httpCode int, code int, obj any) {
c.Header(Status, string(Success))
c.JSON(httpCode, gin.H{
"code": code,
"data": obj,
})
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lipore/plume.git
git@gitee.com:lipore/plume.git
lipore
plume
plume
v1.7.10

搜索帮助