From 227b9e0803f975706daf4fe3981f175af103bf1d Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Sun, 21 Jul 2024 11:00:08 +0800 Subject: [PATCH] =?UTF-8?q?tool=E6=B7=BB=E5=8A=A0redis=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E5=8F=8A=E6=9F=A5=E7=9C=8B=EF=BC=8C=E5=B9=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/tool.go | 34 +++++++++++++++++++- main.go | 3 +- service/toolService.go | 70 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 service/toolService.go diff --git a/handler/tool.go b/handler/tool.go index 4d585de..e566e76 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -5,6 +5,7 @@ import ( "net/http" "videoplayer/dao" "videoplayer/proto" + "videoplayer/service" ) type SetRedisReq struct { @@ -17,6 +18,7 @@ type SetRedisReq struct { func SetUpToolGroup(router *gin.Engine) { toolGroup := router.Group("/tool") toolGroup.POST("/set_redis", SetRedis) + toolGroup.POST("/get_redis", GetRedis) } func SetRedis(c *gin.Context) { @@ -31,7 +33,37 @@ func SetRedis(c *gin.Context) { //解析请求参数 var req SetRedisReq if err := c.ShouldBind(&req); err == nil { - + var code int + var message string + if req.Option == "list" { + code, message = service.SetToolRedisList(req.Key, req.Value, req.Expire) + } else if req.Option == "set" { + code, message = service.SetToolRedisSet(req.Key, req.Value, req.Expire) + } else if req.Option == "kv" { + code, message = service.SetToolRedisKV(req.Key, req.Value, req.Expire) + } + c.JSON(http.StatusOK, gin.H{"code": code, "message": message}) + } else { + c.JSON(http.StatusOK, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"}) + return + } +} + +func GetRedis(c *gin.Context) { + //先查看是否有权限 + id, _ := c.Get("id") + id1 := int(id.(float64)) + user := dao.FindUserByUserID(id1) + if user.Redis == false { + c.JSON(http.StatusOK, gin.H{"error": "no redis Permissions", "code": proto.NoRedisPermissions, "message": "failed"}) + return + } + //解析请求参数 + var req SetRedisReq + if err := c.ShouldBind(&req); err == nil { + code, message := service.GetToolRedis(req.Key) + req.Value = message + c.JSON(http.StatusOK, gin.H{"code": code, "message": message, "data": req}) } else { c.JSON(http.StatusOK, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"}) return diff --git a/main.go b/main.go index 60a2d38..f0afcfc 100644 --- a/main.go +++ b/main.go @@ -27,7 +27,8 @@ func main() { handler.SetUpDeviceGroup(r) // Device handler.SetUpIMGroup(r) // IM handler.SetUpCIDGroup(r) // CID,持续集成、部署 - r.Run(":8083") // listen and serve on 0.0.0.0:8082 + handler.SetUpToolGroup(r) // Tool + r.Run(":8083") // listen and serve on 0.0.0.0:8083 defer dao.Close() defer worker.CloseRedis() } diff --git a/service/toolService.go b/service/toolService.go new file mode 100644 index 0000000..bc78889 --- /dev/null +++ b/service/toolService.go @@ -0,0 +1,70 @@ +package service + +import ( + "time" + "videoplayer/proto" + "videoplayer/worker" +) + +func SetToolRedisList(key string, value string, expire int) (code int, message string) { + if expire == 0 { + if worker.PushRedisList(key, value) { + return proto.SuccessCode, "success" + } else { + return proto.OperationFailed, "push redis list failed" + } + } else if expire > 0 { + if worker.PushRedisListWithExpire(key, value, time.Duration(expire)) { + return proto.SuccessCode, "success" + } else { + return proto.OperationFailed, "push redis list with expire failed" + } + } else { + return proto.ParameterError, "expire time can not be negative" + } +} + +func SetToolRedisSet(key string, value string, expire int) (code int, message string) { + if expire == 0 { + if worker.SetRedis(key, value) { + return proto.SuccessCode, "success" + } else { + return proto.OperationFailed, "set redis failed" + } + } else if expire > 0 { + if worker.SetRedisWithExpire(key, value, time.Duration(expire)) { + return proto.SuccessCode, "success" + } else { + return proto.OperationFailed, "set redis with expire failed" + } + } else { + return proto.ParameterError, "expire time can not be negative" + } +} + +func SetToolRedisKV(key string, value string, expire int) (code int, message string) { + if expire == 0 { + if worker.SetRedis(key, value) { + return proto.SuccessCode, "success" + } else { + return proto.OperationFailed, "set redis failed" + } + } else if expire > 0 { + if worker.SetRedisWithExpire(key, value, time.Duration(expire)) { + return proto.SuccessCode, "success" + } else { + return proto.OperationFailed, "set redis with expire failed" + } + } else { + return proto.ParameterError, "expire time can not be negative" + } +} + +func GetToolRedis(key string) (code int, message string) { + val := worker.GetRedis(key) + if val == "" { + return proto.OperationFailed, "get redis failed" + } else { + return proto.SuccessCode, val + } +}