/
redis.go
61 lines (53 loc) · 1.32 KB
/
redis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package handlers
import (
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/yufeifly/migrator/redis"
"github.com/yufeifly/migrator/utils"
"net/http"
)
// Get redis get handler
func Get(c *gin.Context) {
key := c.Query("Key")
cid := c.Query("ContainerID") // of worker
val, err := redis.Get(cid, key)
if err != nil {
logrus.Errorf("%s, err: %v", "redis.Get", err)
utils.ReportErr(c, http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, val)
}
// Set redis set handler
func Set(c *gin.Context) {
key := c.PostForm("Key")
val := c.PostForm("Value")
cid := c.PostForm("ContainerID")
logrus.WithFields(logrus.Fields{
"key": key,
"value": val,
}).Debug("about to set pair")
err := redis.Set(cid, key, val)
if err != nil {
logrus.WithFields(logrus.Fields{
"key": key,
"value": val,
}).Error("set pair failed")
utils.ReportErr(c, http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, gin.H{"result": "success"})
}
func Delete(c *gin.Context) {
key := c.PostForm("Key")
cid := c.PostForm("ContainerID")
err := redis.Delete(cid, key)
if err != nil {
logrus.WithFields(logrus.Fields{
"key": key,
}).Error("delete pair failed")
utils.ReportErr(c, http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, gin.H{"result": "success"})
}