-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.go
126 lines (114 loc) · 2.93 KB
/
handler.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"log"
"net/http"
"strconv"
as "github.com/aerospike/aerospike-client-go"
"github.com/gin-gonic/gin"
)
const (
defaultNS = "test"
defaultSet = "test_set"
)
//SpikeObject would have a body of POST APIs
type SpikeObject struct {
Namespace string `form:"namespace" json:"namespace"`
Set string `form:"set" json:"set"`
Key string `form:"key" json:"key" binding:"required"`
Record as.BinMap `form:"record" json:"record" binding:"required"`
}
//Connect creates a connection to aerospike
func Connect(c *gin.Context) {
host := c.DefaultQuery("host", "127.0.0.1")
portString := c.Query("port")
port, _ := strconv.Atoi(portString)
namespaces, err := Init(host, port)
if err != nil {
log.Println(err)
c.HTML(http.StatusInternalServerError, "index.tmpl", gin.H{
"message": err.Error(),
})
} else {
log.Printf("host: %v port: %v", host, port)
c.HTML(http.StatusOK, "operation.tmpl", gin.H{
"message": "Connected",
"namespaces": namespaces,
})
}
}
//GetRecord gets a record from aerospike
func GetRecord(c *gin.Context) {
namespace := c.DefaultQuery("namespace", defaultNS)
set := c.DefaultQuery("set", defaultSet)
key := c.Param("key")
record, err := GetRec(namespace, set, key)
if err != nil {
log.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
} else if record == nil {
c.JSON(http.StatusNotFound, gin.H{
"message": "record not found",
})
} else {
bins := ConvertRecord(record.Bins)
c.JSON(http.StatusOK, gin.H{
"key": key,
"record": bins,
})
}
}
//DeleteRecord deletes a record from aerospike
func DeleteRecord(c *gin.Context) {
namespace := c.DefaultQuery("namespace", defaultNS)
set := c.DefaultQuery("set", defaultSet)
key := c.Param("key")
existed, err := DeleteRec(namespace, set, key)
if err != nil {
log.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
} else if existed == true {
c.JSON(http.StatusOK, gin.H{
"message": "record deleted",
})
} else {
c.JSON(http.StatusNotFound, gin.H{
"message": "record didn't exist",
})
}
}
//AddRecord adds a record to aerospike
func AddRecord(c *gin.Context) {
var form SpikeObject
bindErr := c.Bind(&form)
if bindErr != nil {
log.Println("bind error", bindErr)
c.JSON(http.StatusInternalServerError, gin.H{
"message": bindErr.Error(),
})
} else {
log.Printf("namespace: %v set: %v", form.Namespace, form.Set)
if form.Namespace == "" {
form.Namespace = defaultNS
}
if form.Set == "" {
form.Set = defaultSet
}
err := PutRec(form.Namespace, form.Set, form.Key, form.Record)
if err != nil {
log.Println("error while adding", err)
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
} else {
log.Printf("key: %v record: %v", form.Key, form.Record)
c.JSON(http.StatusOK, gin.H{
"key": form.Key,
"record": form.Record,
})
}
}
}