tool添加redis操作及查看,并添加请求
This commit is contained in:
parent
ee478c105f
commit
227b9e0803
|
|
@ -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
|
||||
|
|
|
|||
3
main.go
3
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()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue