-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
63 lines (53 loc) · 1.17 KB
/
routes.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
package main
import (
"cloud/database/db"
"fmt"
"github.com/gin-gonic/gin"
"strings"
)
func HandlePing() gin.HandlerFunc {
return func(ctx *gin.Context) {
ctx.JSON(200, gin.H{
"message": "Hello World!",
})
}
}
func HandleSet(kvStore *db.KVStore) gin.HandlerFunc {
return func(ctx *gin.Context) {
var data map[string]string
if err := ctx.BindJSON(&data); err != nil {
ctx.JSON(400, gin.H{
"message": "Invalid request body",
})
return
}
key := data["key"]
value := data["value"]
// convert comma separated string to array of string
values := strings.Split(value, ",")
if err := kvStore.Set(key, values); err != nil {
ctx.JSON(500, gin.H{
"message": fmt.Sprintf("Error setting value %s", err),
})
return
}
ctx.JSON(200, gin.H{
"message": "Value set successfully",
})
}
}
func HandleGet(kvStore *db.KVStore) gin.HandlerFunc {
return func(ctx *gin.Context) {
key := ctx.Query("key")
result, err := kvStore.Get(key)
if err != nil {
ctx.JSON(500, gin.H{
"message": fmt.Sprintf("Error getting value %s", err),
})
}
ctx.JSON(200, gin.H{
"data": result,
"message": "Values get successfully",
})
}
}