refactor(后端): 重构后端API响应数据格式,统一为Code Message Data
This commit is contained in:
@@ -3,6 +3,7 @@ package controller
|
|||||||
import (
|
import (
|
||||||
"dypid/internal/db"
|
"dypid/internal/db"
|
||||||
"dypid/internal/global"
|
"dypid/internal/global"
|
||||||
|
"dypid/internal/model"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@@ -11,7 +12,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func ListTokenHandler(c *gin.Context) {
|
func ListTokenHandler(c *gin.Context) {
|
||||||
c.JSON(http.StatusOK, gin.H{"result": db.ListToken()})
|
c.JSON(http.StatusOK, model.APIResponse{
|
||||||
|
Code: 200,
|
||||||
|
Message: "获取所有token成功",
|
||||||
|
Data: db.ListToken(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateTokenHandler(c *gin.Context) {
|
func CreateTokenHandler(c *gin.Context) {
|
||||||
@@ -23,24 +28,37 @@ func CreateTokenHandler(c *gin.Context) {
|
|||||||
Notes string `form:"notes"`
|
Notes string `form:"notes"`
|
||||||
}{}
|
}{}
|
||||||
if err := c.ShouldBindQuery(&input); err != nil {
|
if err := c.ShouldBindQuery(&input); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "参数不能为空 " + err.Error()})
|
c.JSON(http.StatusBadRequest, model.APIResponse{
|
||||||
|
Code: 400,
|
||||||
|
Message: "参数不能为空 " + err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//检查Token是否存在
|
//检查Token是否存在
|
||||||
if db.CheckToken(input.Token) {
|
if db.CheckToken(input.Token) {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "创建Token失败,Token已经存在"})
|
c.JSON(http.StatusBadRequest, model.APIResponse{
|
||||||
|
Code: 400,
|
||||||
|
Message: "创建Token失败,Token已经存在,请勿重复创建",
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//创建Token
|
//创建Token
|
||||||
err := db.CreateToken(input.Token, input.DedupObject, input.DataFormat, input.Notes)
|
err := db.CreateToken(input.Token, input.DedupObject, input.DataFormat, input.Notes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
c.JSON(http.StatusInternalServerError, model.APIResponse{
|
||||||
|
Code: 500,
|
||||||
|
Message: err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//返回
|
//返回
|
||||||
c.JSON(http.StatusOK, gin.H{"result": "ok"})
|
c.JSON(http.StatusOK, model.APIResponse{
|
||||||
|
Code: 200,
|
||||||
|
Message: "创建Token成功",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateTokenHandler(c *gin.Context) {
|
func UpdateTokenHandler(c *gin.Context) {
|
||||||
@@ -51,22 +69,35 @@ func UpdateTokenHandler(c *gin.Context) {
|
|||||||
Notes string `form:"notes"`
|
Notes string `form:"notes"`
|
||||||
}{}
|
}{}
|
||||||
if err := c.ShouldBindQuery(&input); err != nil {
|
if err := c.ShouldBindQuery(&input); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "参数不能为空 " + err.Error()})
|
c.JSON(http.StatusBadRequest, model.APIResponse{
|
||||||
|
Code: 400,
|
||||||
|
Message: "参数不能为空 " + err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//检查Token是否存在
|
//检查Token是否存在
|
||||||
if !db.CheckToken(input.Token) {
|
if !db.CheckToken(input.Token) {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "更改失败,Token不存在"})
|
c.JSON(http.StatusBadRequest, model.APIResponse{
|
||||||
|
Code: 400,
|
||||||
|
Message: "更改失败,Token不存在",
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err := db.UpdateToken(input.Token, input.DedupObject, input.DataFormat, input.Notes)
|
err := db.UpdateToken(input.Token, input.DedupObject, input.DataFormat, input.Notes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
c.JSON(http.StatusInternalServerError, model.APIResponse{
|
||||||
|
Code: 500,
|
||||||
|
Message: err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{"result": "ok"})
|
c.JSON(http.StatusOK, model.APIResponse{
|
||||||
|
Code: 200,
|
||||||
|
Message: "更改Token成功",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteTokenHandler(c *gin.Context) {
|
func DeleteTokenHandler(c *gin.Context) {
|
||||||
@@ -74,22 +105,35 @@ func DeleteTokenHandler(c *gin.Context) {
|
|||||||
Token string `form:"token" binding:"required"`
|
Token string `form:"token" binding:"required"`
|
||||||
}{}
|
}{}
|
||||||
if err := c.ShouldBindQuery(&input); err != nil {
|
if err := c.ShouldBindQuery(&input); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "参数不能为空 " + err.Error()})
|
c.JSON(http.StatusBadRequest, model.APIResponse{
|
||||||
|
Code: 400,
|
||||||
|
Message: "参数不能为空 " + err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//检查Token是否存在
|
//检查Token是否存在
|
||||||
if !db.CheckToken(input.Token) {
|
if !db.CheckToken(input.Token) {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "删除Token失败,Token不存在"})
|
c.JSON(http.StatusBadRequest, model.APIResponse{
|
||||||
|
Code: 400,
|
||||||
|
Message: "删除Token失败,Token不存在",
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err := db.DeleteToken(input.Token)
|
err := db.DeleteToken(input.Token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
c.JSON(http.StatusInternalServerError, model.APIResponse{
|
||||||
|
Code: 500,
|
||||||
|
Message: err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{"result": "ok"})
|
c.JSON(http.StatusOK, model.APIResponse{
|
||||||
|
Code: 200,
|
||||||
|
Message: "删除Token成功",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTokenInfoHandler(c *gin.Context) {
|
func GetTokenInfoHandler(c *gin.Context) {
|
||||||
@@ -97,23 +141,36 @@ func GetTokenInfoHandler(c *gin.Context) {
|
|||||||
Token string `form:"token" binding:"required"`
|
Token string `form:"token" binding:"required"`
|
||||||
}{}
|
}{}
|
||||||
if err := c.ShouldBindQuery(&input); err != nil {
|
if err := c.ShouldBindQuery(&input); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Token不能为空"})
|
c.JSON(http.StatusBadRequest, model.APIResponse{
|
||||||
|
Code: 400,
|
||||||
|
Message: "参数不能为空 " + err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//检查Token是否存在
|
//检查Token是否存在
|
||||||
if !db.CheckToken(input.Token) {
|
if !db.CheckToken(input.Token) {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "获取信息失败,Token不存在"})
|
c.JSON(http.StatusBadRequest, model.APIResponse{
|
||||||
|
Code: 400,
|
||||||
|
Message: "获取信息失败,Token不存在",
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
dedupObject, err := db.GetDedupObject(input.Token)
|
dedupObject, err := db.GetDedupObject(input.Token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取去重对象失败 " + err.Error()})
|
c.JSON(http.StatusInternalServerError, model.APIResponse{
|
||||||
|
Code: 500,
|
||||||
|
Message: "获取去重对象失败 " + err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
dataFormat, err := db.GetDataFormat(input.Token)
|
dataFormat, err := db.GetDataFormat(input.Token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取数据格式失败 " + err.Error()})
|
c.JSON(http.StatusInternalServerError, model.APIResponse{
|
||||||
|
Code: 500,
|
||||||
|
Message: "获取数据格式失败 " + err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +187,11 @@ func GetTokenInfoHandler(c *gin.Context) {
|
|||||||
output.DedupItemsNumber = global.RDB.CFInfo(global.RCtx, "dedup:"+input.Token+":"+dedupObject).Val().NumItemsInserted
|
output.DedupItemsNumber = global.RDB.CFInfo(global.RCtx, "dedup:"+input.Token+":"+dedupObject).Val().NumItemsInserted
|
||||||
output.CacheListNumber = global.RDB.LLen(global.RCtx, "list:"+input.Token).Val()
|
output.CacheListNumber = global.RDB.LLen(global.RCtx, "list:"+input.Token).Val()
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{"result": output})
|
c.JSON(http.StatusOK, model.APIResponse{
|
||||||
|
Code: 200,
|
||||||
|
Message: "获取Token信息成功",
|
||||||
|
Data: output,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteTokenInfoHandler(c *gin.Context) {
|
func DeleteTokenInfoHandler(c *gin.Context) {
|
||||||
@@ -142,24 +203,29 @@ func DeleteTokenInfoHandler(c *gin.Context) {
|
|||||||
BothNumber string `form:"both_number"`
|
BothNumber string `form:"both_number"`
|
||||||
}{}
|
}{}
|
||||||
if err := c.ShouldBindQuery(&input); err != nil {
|
if err := c.ShouldBindQuery(&input); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Token不能为空"})
|
c.JSON(http.StatusBadRequest, model.APIResponse{
|
||||||
return
|
Code: 400,
|
||||||
}
|
Message: "参数不能为空 " + err.Error(),
|
||||||
//检查Token是否存在
|
})
|
||||||
if !db.CheckToken(input.Token) {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "删除Token失败,Token不存在"})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//检查token是否存在
|
//检查Token是否存在
|
||||||
_, err := db.GetDedupObject(input.Token)
|
if !db.CheckToken(input.Token) {
|
||||||
if err != nil {
|
c.JSON(http.StatusBadRequest, model.APIResponse{
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Token不存在" + err.Error()})
|
Code: 400,
|
||||||
|
Message: "删除Token信息失败,Token不存在",
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//获取去重对象
|
||||||
dedupObject, err := db.GetDedupObject(input.Token)
|
dedupObject, err := db.GetDedupObject(input.Token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, model.APIResponse{
|
||||||
|
Code: 400,
|
||||||
|
Message: "获取去重对象失败 " + err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,7 +239,10 @@ func DeleteTokenInfoHandler(c *gin.Context) {
|
|||||||
default:
|
default:
|
||||||
num, err := strconv.Atoi(input.DedupBF)
|
num, err := strconv.Atoi(input.DedupBF)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "dedup_bf数量设置错误 " + err.Error()})
|
c.JSON(http.StatusBadRequest, model.APIResponse{
|
||||||
|
Code: 400,
|
||||||
|
Message: "dedup_bf数量设置错误 " + err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,7 +265,10 @@ func DeleteTokenInfoHandler(c *gin.Context) {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
c.JSON(http.StatusInternalServerError, model.APIResponse{
|
||||||
|
Code: 500,
|
||||||
|
Message: "删除去重对象失败 " + err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,7 +285,10 @@ func DeleteTokenInfoHandler(c *gin.Context) {
|
|||||||
default:
|
default:
|
||||||
num, err := strconv.Atoi(input.CacheList)
|
num, err := strconv.Atoi(input.CacheList)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "cache_list数量设置错误 " + err.Error()})
|
c.JSON(http.StatusBadRequest, model.APIResponse{
|
||||||
|
Code: 400,
|
||||||
|
Message: "cache_list数量设置错误 " + err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -226,7 +301,10 @@ func DeleteTokenInfoHandler(c *gin.Context) {
|
|||||||
default:
|
default:
|
||||||
num, err := strconv.Atoi(input.BothNumber)
|
num, err := strconv.Atoi(input.BothNumber)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "both_number数量设置错误 " + err.Error()})
|
c.JSON(http.StatusBadRequest, model.APIResponse{
|
||||||
|
Code: 500,
|
||||||
|
Message: "both_number数量设置错误 " + err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,12 +326,18 @@ func DeleteTokenInfoHandler(c *gin.Context) {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
c.JSON(http.StatusInternalServerError, model.APIResponse{
|
||||||
|
Code: 500,
|
||||||
|
Message: "删除Token信息失败 " + err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//输出信息
|
//输出信息
|
||||||
c.JSON(http.StatusOK, gin.H{"result": "ok"})
|
c.JSON(http.StatusOK, model.APIResponse{
|
||||||
|
Code: 200,
|
||||||
|
Message: "删除Token信息成功",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
type APIResponse struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Data any `json:"data"`
|
||||||
|
}
|
||||||
@@ -22,7 +22,10 @@ func main() {
|
|||||||
//初始化一个http服务对象
|
//初始化一个http服务对象
|
||||||
gin.SetMode(config.APPConfig.RunMode)
|
gin.SetMode(config.APPConfig.RunMode)
|
||||||
r := gin.New()
|
r := gin.New()
|
||||||
r.Use(gin.Recovery())
|
if config.APPConfig.RunMode == "debug" {
|
||||||
|
r.Use(gin.Logger()) //日志处理
|
||||||
|
}
|
||||||
|
r.Use(gin.Recovery()) //错误处理
|
||||||
r.Use(cors.Default()) //跨域设置
|
r.Use(cors.Default()) //跨域设置
|
||||||
|
|
||||||
//注册网页服务(Vue)
|
//注册网页服务(Vue)
|
||||||
|
|||||||
Reference in New Issue
Block a user