feat: 首次提交,添加Web界面和Redis支持的数据管理系统
This commit is contained in:
122
controller/tokenController.go
Normal file
122
controller/tokenController.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"dypid/db"
|
||||
"dypid/global"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func ListTokenHandler(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"result": db.ListToken()})
|
||||
}
|
||||
|
||||
func CreateTokenHandler(c *gin.Context) {
|
||||
if c.Query("token") == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "token不能为空"})
|
||||
return
|
||||
} else if c.Query("dedup_object") == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "dedup_object不能为空"})
|
||||
return
|
||||
}
|
||||
|
||||
err := db.CreateToken(c.Query("token"), c.Query("dedup_object"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"result": "ok"})
|
||||
}
|
||||
|
||||
func UpdateTokenHandler(c *gin.Context) {
|
||||
if c.Query("token") == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "token不能为空"})
|
||||
return
|
||||
} else if c.Query("dedup_object") == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "dedup_object不能为空"})
|
||||
return
|
||||
}
|
||||
|
||||
err := db.UpdateToken(c.Query("token"), c.Query("dedup_object"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"result": "ok"})
|
||||
}
|
||||
|
||||
func DeleteTokenHandler(c *gin.Context) {
|
||||
if c.Query("token") == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "token不能为空"})
|
||||
return
|
||||
}
|
||||
|
||||
err := db.DeleteToken(c.Query("token"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"result": "ok"})
|
||||
}
|
||||
|
||||
func GetTokenInfoHandler(c *gin.Context) {
|
||||
input := struct {
|
||||
Token string `form:"token" binding:"required"`
|
||||
}{}
|
||||
if err := c.ShouldBindQuery(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Token不能为空"})
|
||||
return
|
||||
}
|
||||
|
||||
dedupObject, err := db.GetDedupObject(input.Token)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取去重对象失败 " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
output := struct {
|
||||
Token string `json:"token"`
|
||||
DedupObject string `json:"dedup_object"`
|
||||
DedupItemsNumber int64 `json:"dedup_items_number"`
|
||||
CacheListNumber int64 `json:"cache_list_number"`
|
||||
}{}
|
||||
output.Token = input.Token
|
||||
output.DedupObject = dedupObject
|
||||
output.DedupItemsNumber = global.RDB.BFCard(global.RCtx, "dedup:"+input.Token+":"+dedupObject).Val()
|
||||
output.CacheListNumber = global.RDB.LLen(global.RCtx, "list:"+input.Token).Val()
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"result": output})
|
||||
}
|
||||
|
||||
func DeleteTokenInfoHandler(c *gin.Context) {
|
||||
//解析输入数据
|
||||
input := struct {
|
||||
Token string `form:"token" binding:"required"`
|
||||
DedupBF bool `form:"dedup_bf"`
|
||||
CacheList bool `form:"cache_list"`
|
||||
}{}
|
||||
if err := c.ShouldBindQuery(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Token不能为空"})
|
||||
return
|
||||
}
|
||||
|
||||
//检查token是否存在
|
||||
_, err := db.GetDedupObject(input.Token)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Token不存在" + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
//删除去重对象
|
||||
if input.DedupBF {
|
||||
keys := global.RDB.Keys(global.RCtx, "dedup:"+input.Token+":*").Val()
|
||||
global.RDB.Del(global.RCtx, keys...)
|
||||
}
|
||||
if input.CacheList {
|
||||
global.RDB.Del(global.RCtx, "list:"+input.Token)
|
||||
}
|
||||
|
||||
//输出信息
|
||||
c.JSON(http.StatusOK, gin.H{"result": "ok"})
|
||||
}
|
Reference in New Issue
Block a user