feat: 首次提交,添加Web界面和Redis支持的数据管理系统

This commit is contained in:
2025-09-01 00:45:37 +08:00
parent 7abb872b2c
commit b9fbe07b70
30 changed files with 4970 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
package controller
import (
"dypid/db"
"dypid/global"
"dypid/model"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
)
func ReadDataHandler(c *gin.Context) {
lLen := global.RDB.LLen(global.RCtx, fmt.Sprintf("list:%s", c.Query("token")))
if lLen.Val() == 0 {
c.JSON(200, gin.H{"result": "数据库没有数据"})
return
}
retData := global.RDB.BLPop(global.RCtx, 0, fmt.Sprintf("list:%s", c.Query("token")))
newData := model.Data{}
err := json.Unmarshal([]byte(retData.Val()[1]), &newData)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
c.JSON(200, gin.H{"result": newData})
}
func WriteDataHandler(c *gin.Context) {
data := model.Data{}
if err := c.BindQuery(&data); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
dedupObject, err := db.GetDedupObject(data.Token)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
err = createBF(fmt.Sprintf("dedup:%s:%s", data.Token, dedupObject), 0.01, 100000000)
if err != nil && err.Error() != "ERR item exists" {
c.JSON(400, gin.H{"error": err.Error()})
return
}
exists := global.RDB.BFExists(global.RCtx, fmt.Sprintf("dedup:%s:%s", data.Token, dedupObject), c.Query(dedupObject))
if exists.Val() {
return
}
marshal, err := json.Marshal(data)
if err != nil {
return
}
global.RDB.LPush(global.RCtx, fmt.Sprintf("list:%s", data.Token), marshal)
global.RDB.BFAdd(global.RCtx, fmt.Sprintf("dedup:%s:%s", data.Token, dedupObject), c.Query(dedupObject))
c.JSON(200, gin.H{"result": "ok"})
}
func createBF(bloomFilter string, errorRate float64, capacity int64) error {
_, err := global.RDB.BFReserve(global.RCtx, bloomFilter, errorRate, capacity).Result()
return err
}

View 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"})
}